use of com.google.zxing.RGBLuminanceSource in project BGAQRCode-Android by bingoogolapple.
the class QRCodeDecoder method syncDecodeQRCode.
/**
* 同步解析bitmap二维码。该方法是耗时操作,请在子线程中调用。
*
* @param bitmap 要解析的二维码图片
* @return 返回二维码图片里的内容 或 null
*/
public static String syncDecodeQRCode(Bitmap bitmap) {
Result result = null;
RGBLuminanceSource source = null;
try {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
source = new RGBLuminanceSource(width, height, pixels);
result = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
if (source != null) {
try {
result = new MultiFormatReader().decode(new BinaryBitmap(new GlobalHistogramBinarizer(source)), HINTS);
return result.getText();
} catch (Throwable e2) {
e2.printStackTrace();
}
}
return null;
}
}
use of com.google.zxing.RGBLuminanceSource 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.RGBLuminanceSource in project SimplifyReader by chentao0707.
the class DecodeUtils method decodeWithZxing.
public String decodeWithZxing(Bitmap bitmap) {
MultiFormatReader multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(changeZXingDecodeDataMode());
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
Result rawResult = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
if (source != null) {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(binaryBitmap);
} catch (ReaderException re) {
// continue
} finally {
multiFormatReader.reset();
}
}
return rawResult != null ? rawResult.getText() : null;
}
Aggregations