use of com.google.zxing.ChecksumException in project my-hardware by kellysong.
the class CaptureActivity method scanningImage.
/**
* 图片识别
*
* @param path
* @return
*/
public static Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
// 设置二维码内容的编码
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = true; // 先获取原大小
// 获取新的大小
options.inJustDecodeBounds = false;
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
Bitmap scanBitmap = BitmapFactory.decodeFile(path, options);
int[] intArray = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}
use of com.google.zxing.ChecksumException in project MyBaseApplication by BanShouWeng.
the class AlbumDecoding method scanningImage.
/**
* 扫描二维码图片的方法
*
* @param path 图片路径
* @return 扫描结果
*/
private static Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
// 设置二维码内容的编码
hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
BitmapFactory.Options options = new BitmapFactory.Options();
// 先获取原大小
options.inJustDecodeBounds = true;
// 扫描图片的bitmap
Bitmap scanBitmap = BitmapFactory.decodeFile(path, options);
// 获取新的大小
options.inJustDecodeBounds = false;
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
int[] intArray = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
Result result = null;
// 二维码解码
QRCodeReader qrCodeReader = new QRCodeReader();
try {
result = qrCodeReader.decode(bitmap1, hints);
} catch (NotFoundException | ChecksumException | FormatException e) {
e.printStackTrace();
}
if (null == result) {
// DataMatrix解码
DataMatrixReader dataMatrixReader = new DataMatrixReader();
try {
result = dataMatrixReader.decode(bitmap1, hints);
} catch (NotFoundException | ChecksumException | FormatException e) {
e.printStackTrace();
}
}
return result;
}
Aggregations