use of com.google.zxing.common.HybridBinarizer 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.common.HybridBinarizer in project zxing by zxing.
the class DecodeServlet method processImage.
private static void processImage(BufferedImage image, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Collection<Result> results = new ArrayList<>(1);
try {
Reader reader = new MultiFormatReader();
ReaderException savedException = null;
try {
// Look for multiple barcodes
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
if (theResults != null) {
results.addAll(Arrays.asList(theResults));
}
} catch (ReaderException re) {
savedException = re;
}
if (results.isEmpty()) {
try {
// Look for pure barcode
Result theResult = reader.decode(bitmap, HINTS_PURE);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
// Look for normal barcode in photo
Result theResult = reader.decode(bitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
// Try again with other binarizer
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
Result theResult = reader.decode(hybridBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
} catch (FormatException | ChecksumException e) {
log.info(e.toString());
errorResponse(request, response, "format");
} catch (ReaderException e) {
// Including NotFoundException
log.info(e.toString());
errorResponse(request, response, "notfound");
}
return;
}
} catch (RuntimeException re) {
// Call out unexpected errors in the log clearly
log.log(Level.WARNING, "Unexpected exception from library", re);
throw new ServletException(re);
}
String fullParameter = request.getParameter("full");
boolean minimalOutput = fullParameter != null && !Boolean.parseBoolean(fullParameter);
if (minimalOutput) {
response.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
try (Writer out = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
for (Result result : results) {
out.write(result.getText());
out.write('\n');
}
}
} else {
request.setAttribute("results", results);
request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
}
}
use of com.google.zxing.common.HybridBinarizer in project zxing by zxing.
the class MultiTestCase method testMulti.
@Test
public void testMulti() throws Exception {
// Very basic test for now
Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-1");
Path testImage = testBase.resolve("1.png");
BufferedImage image = ImageIO.read(testImage.toFile());
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Result[] results = reader.decodeMultiple(bitmap);
assertNotNull(results);
assertEquals(2, results.length);
assertEquals("031415926531", results[0].getText());
assertEquals(BarcodeFormat.UPC_A, results[0].getBarcodeFormat());
assertEquals("www.airtable.com/jobs", results[1].getText());
assertEquals(BarcodeFormat.QR_CODE, results[1].getBarcodeFormat());
}
use of com.google.zxing.common.HybridBinarizer in project zxing by zxing.
the class BenchmarkAsyncTask method decode.
private static BenchmarkItem decode(MultiFormatReader reader, File file) {
Bitmap imageBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
if (imageBitmap == null) {
Log.e(TAG, "Couldn't open " + file);
return null;
}
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
int[] pixels = new int[width * height];
imageBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BenchmarkItem item = new BenchmarkItem(file, RUNS);
for (int x = 0; x < RUNS; x++) {
boolean success;
Result result = null;
// Using this call instead of getting the time should eliminate a lot of variability due to
// scheduling and what else is happening in the system.
long now = Debug.threadCpuTimeNanos();
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = reader.decodeWithState(bitmap);
success = true;
} catch (ReaderException ignored) {
success = false;
}
now = Debug.threadCpuTimeNanos() - now;
if (x == 0) {
item.setDecoded(success);
item.setFormat(result != null ? result.getBarcodeFormat() : null);
}
item.addResult((int) (now / 1000));
}
return item;
}
use of com.google.zxing.common.HybridBinarizer 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;
}
Aggregations