private String (String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
BitmapFactory.Options options = new BitmapFactory.Options();
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));
QRCodeReader reader = new QRCodeReader();
try {
Result decode = reader.decode(bitmap1, hints);
return recode(decode.toString());
} catch (NotFoundException | com.google.zxing.FormatException | ChecksumException e) {
e.printStackTrace();
}
return null;
}
private String recode(String str) {
String formart = "";
try {
boolean ISO = Charset.forName("ISO-8859-1").newEncoder().canEncode(str);
if (ISO) {
formart = new String(str.getBytes("ISO-8859-1"), "GB2312");
} else {
formart = str;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return formart;
}
近期评论