/** 解析QRCode二维码 */ @SuppressWarnings("unchecked") public void decode(File file) { try { BufferedImage image; try { image = ImageIO.read(file); if (image == null) { System.out.println("Could not decode image"); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; @SuppressWarnings("rawtypes") Hashtable hints = new Hashtable(); // 解码设置编码方式为:utf-8 hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); System.out.println("解析后内容:" + resultStr); } catch (IOException ioe) { System.out.println(ioe.toString()); } catch (ReaderException re) { System.out.println(re.toString()); } } catch (Exception ex) { System.out.println(ex.toString()); } }
private static void assertCorrectImage2string(String path, String expected) throws IOException, NotFoundException { File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result result; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); result = rssExpandedReader.decodeRow(rowNumber, row, null); } catch (ReaderException re) { fail(re.toString()); return; } assertSame(BarcodeFormat.RSS_EXPANDED, result.getBarcodeFormat()); assertEquals(expected, result.getText()); }
private static String getDecodeText(File file) { BufferedImage image; try { image = ImageReader.readImage(file); } catch (IOException ioe) { return ioe.toString(); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; try { result = new MultiFormatReader().decode(bitmap); } catch (ReaderException re) { return re.toString(); } return String.valueOf(result.getText()); }
public String attemptDecodePicture(Bitmap thumbnail) { if (thumbnail == null) { Log.d(TAG, "No picture selected"); } else { Log.d(TAG, "Picture selected"); Result rawResult = null; Reader reader = new QRCodeReader(); int w = thumbnail.getWidth(); int h = thumbnail.getHeight(); int maxOneDimension = 500; if (w * h > maxOneDimension * maxOneDimension) { // too big, reduce float bitmapRatio = (float) w / (float) h; if (bitmapRatio > 0) { w = maxOneDimension; h = (int) (w / bitmapRatio); } else { h = maxOneDimension; w = (int) (h * bitmapRatio); } thumbnail = Bitmap.createScaledBitmap(thumbnail, w, h, true); } int[] pixels = new int[w * h]; thumbnail.getPixels(pixels, 0, w, 0, 0, w, h); RGBLuminanceSource source = new RGBLuminanceSource(w, h, pixels); if (source.getMatrix() != null) { BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = reader.decode(bitmap); } catch (ReaderException re) { re.printStackTrace(); } finally { reader.reset(); } } if (rawResult != null) { Log.d(TAG, "QR code found " + rawResult.getText()); return rawResult.getText(); } else { Log.d(TAG, "Picture No QR code found"); } } return null; }
/** * 解码 * * @param filePath * @return */ @SuppressWarnings({"rawtypes", "unchecked"}) public static String decode(String filePath) { BufferedImage image; try { image = ImageIO.read(new File(filePath)); } catch (IOException ioe) { return ioe.toString(); } if (image == null) { return "Could not decode image"; } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; try { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, ToolString.encoding); result = new MultiFormatReader().decode(bitmap, hints); } catch (ReaderException re) { return re.toString(); } return result.getText(); }