1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| private String scanAlbum(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; }
|