use of com.google.zxing.PlanarYUVLuminanceSource in project SimplifyReader by chentao0707.
the class DecodeUtils method decodeWithZxing.
public String decodeWithZxing(byte[] data, int width, int height, Rect crop) {
MultiFormatReader multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(changeZXingDecodeDataMode());
Result rawResult = null;
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, crop.left, crop.top, crop.width(), crop.height(), false);
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
// continue
} finally {
multiFormatReader.reset();
}
}
return rawResult != null ? rawResult.getText() : null;
}
use of com.google.zxing.PlanarYUVLuminanceSource in project weex-example by KalicyZhou.
the class DecodeHandler method decode.
/**
* Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
* reuse the same reader objects from one decode to the next.
*
* @param data The YUV preview frame.
* @param width The width of the preview frame.
* @param height The height of the preview frame.
*/
private void decode(byte[] data, int width, int height) {
long start = System.currentTimeMillis();
Result rawResult = null;
// PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;
width = height;
height = tmp;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
// continue
} finally {
multiFormatReader.reset();
}
}
Handler handler = activity.getHandler();
if (rawResult != null) {
// Don't log the barcode contents for security.
long end = System.currentTimeMillis();
Log.d(TAG, "Found barcode in " + (end - start) + " ms");
if (handler != null) {
Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
Bundle bundle = new Bundle();
bundleThumbnail(source, bundle);
message.setData(bundle);
message.sendToTarget();
}
} else {
if (handler != null) {
Message message = Message.obtain(handler, R.id.decode_failed);
message.sendToTarget();
}
}
}
use of com.google.zxing.PlanarYUVLuminanceSource in project BGAQRCode-Android by bingoogolapple.
the class ZXingView method processData.
@Override
public String processData(byte[] data, int width, int height, boolean isRetry) {
String result = null;
Result rawResult = null;
try {
PlanarYUVLuminanceSource source = null;
Rect rect = mScanBoxView.getScanBoxAreaRect(height);
if (rect != null) {
source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
} else {
source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
}
rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source)));
} catch (Exception e) {
e.printStackTrace();
} finally {
mMultiFormatReader.reset();
}
if (rawResult != null) {
result = rawResult.getText();
}
return result;
}
use of com.google.zxing.PlanarYUVLuminanceSource in project SimplifyReader by chentao0707.
the class DecodeHandler method decode.
/**
* Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
* reuse the same reader objects from one decode to the next.
*
* @param data The YUV preview frame.
* @param width The width of the preview frame.
* @param height The height of the preview frame.
*/
private void decode(byte[] data, int width, int height) {
long start = System.currentTimeMillis();
Camera.Size size = activity.getCameraManager().getPreviewSize();
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < size.height; y++) {
for (int x = 0; x < size.width; x++) rotatedData[x * size.height + size.height - y - 1] = data[x + y * size.width];
}
int tmp = size.width;
size.width = size.height;
size.height = tmp;
String resultStr = null;
Rect cropRect = activity.getCropRect();
if (null == cropRect) {
activity.initCrop();
}
cropRect = activity.getCropRect();
TLog.d(TAG, cropRect.toString());
mDecodeUtils.setDataMode(activity.getDataMode());
String zbarStr = mDecodeUtils.decodeWithZbar(rotatedData, size.width, size.height, cropRect);
String zxingStr = mDecodeUtils.decodeWithZxing(rotatedData, size.width, size.height, cropRect);
if (!TextUtils.isEmpty(zbarStr)) {
mDecodeMode = DecodeUtils.DECODE_MODE_ZBAR;
resultStr = zbarStr;
} else if (!TextUtils.isEmpty(zxingStr)) {
mDecodeMode = DecodeUtils.DECODE_MODE_ZXING;
resultStr = zxingStr;
}
Handler handler = activity.getHandler();
if (!TextUtils.isEmpty(resultStr)) {
long end = System.currentTimeMillis();
if (handler != null) {
Message message = Message.obtain(handler, Constants.ID_DECODE_SUCCESS, resultStr);
Bundle bundle = new Bundle();
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(rotatedData, size.width, size.height, cropRect.left, cropRect.top, cropRect.width(), cropRect.height(), false);
bundle.putInt(DecodeThread.DECODE_MODE, mDecodeMode);
bundle.putString(DecodeThread.DECODE_TIME, (end - start) + "ms");
bundleThumbnail(source, bundle);
message.setData(bundle);
message.sendToTarget();
}
} else {
if (handler != null) {
Message message = Message.obtain(handler, Constants.ID_DECODE_FAILED);
message.sendToTarget();
}
}
}
use of com.google.zxing.PlanarYUVLuminanceSource in project barcodescanner by dm77.
the class ZXingScannerView method onPreviewFrame.
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (mResultHandler == null) {
return;
}
try {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
int width = size.width;
int height = size.height;
if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;
width = height;
height = tmp;
data = rotatedData;
}
Result rawResult = null;
PlanarYUVLuminanceSource source = buildLuminanceSource(data, width, height);
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = mMultiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
// continue
} catch (NullPointerException npe) {
// This is terrible
} catch (ArrayIndexOutOfBoundsException aoe) {
} finally {
mMultiFormatReader.reset();
}
}
final Result finalRawResult = rawResult;
if (finalRawResult != null) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
// Stopping the preview can take a little long.
// So we want to set result handler to null to discard subsequent calls to
// onPreviewFrame.
ResultHandler tmpResultHandler = mResultHandler;
mResultHandler = null;
stopCameraPreview();
if (tmpResultHandler != null) {
tmpResultHandler.handleResult(finalRawResult);
}
}
});
} else {
camera.setOneShotPreviewCallback(this);
}
} catch (RuntimeException e) {
// TODO: Terrible hack. It is possible that this method is invoked after camera is released.
Log.e(TAG, e.toString(), e);
}
}
Aggregations