/** * Given an image source, convert to a binary bitmap. * * Override this to use a custom binarizer. * * @param source the image source * @return a BinaryBitmap */ protected BinaryBitmap toBitmap(LuminanceSource source) { return new BinaryBitmap(new HybridBinarizer(source)); }
return matrix; LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); int height = source.getHeight(); subHeight++; int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix); matrix = newMatrix; } else {
return matrix; LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); int height = source.getHeight(); subHeight++; int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix); matrix = newMatrix; } else {
/** * Given an image source, convert to a binary bitmap. * * Override this to use a custom binarizer. * * @param source the image source * @return a BinaryBitmap */ protected BinaryBitmap toBitmap(LuminanceSource source) { return new BinaryBitmap(new HybridBinarizer(source.invert())); } }
private void binarizeEntireImage() throws NotFoundException { if (matrix == null) { LuminanceSource source = getLuminanceSource(); if (source.getWidth() >= MINIMUM_DIMENSION && source.getHeight() >= MINIMUM_DIMENSION) { byte[] luminances = source.getMatrix(); int width = source.getWidth(); int height = source.getHeight(); int subWidth = width >> 3; int subHeight = height >> 3; int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width); matrix = new BitMatrix(width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, matrix); } else { // If the image is too small, fall back to the global histogram approach. matrix = super.getBlackMatrix(); } } }
/** * Given an image source, convert to a binary bitmap. * * Override this to use a custom binarizer. * * @param source the image source * @return a BinaryBitmap */ protected BinaryBitmap toBitmap(LuminanceSource source) { if (isInverted) { isInverted = false; return new BinaryBitmap(new HybridBinarizer(source.invert())); } else { isInverted = true; return new BinaryBitmap(new HybridBinarizer(source)); } }
/** * @param srcImgFilePath * 要解码的图片地址 * @return {Result} */ @SuppressWarnings("finally") public static Result decode(String srcImgFilePath) { Result result = null; BufferedImage image; try { File srcFile = new File(srcImgFilePath); image = ImageIO.read(srcFile); if (null != image) { LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); result = new MultiFormatReader().decode(bitmap, hints); } else { throw new IllegalArgumentException ("Could not decode image."); } } catch (Exception e) { e.printStackTrace(); } finally { return result; } }
/** * 读取二维码信息 * * @param filePath 文件路径 * @param hintMap hintMap * @return 二维码内容 */ private static String readQRCode(File filePath, Map hintMap) { try { BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer( new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath))))); Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap); return qrCodeResult.getText(); } catch (Exception e) { e.printStackTrace(); return null; } }
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer( rgbLuminanceSource)); QRCodeReader qrCodeReader = new QRCodeReader();
final Binarizer binarizer = new HybridBinarizer(source); final BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(mBitmap)))); } catch (Exception e) { e.printStackTrace();
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap);
final Binarizer binarizer = new HybridBinarizer(source); final BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap); bitmap = new BinaryBitmap(new HybridBinarizer(source)); rawResult = multiFormatReader.decodeWithState(bitmap); } catch (Exception e) {
private String decodeQrPng(String encodedQrCode) throws IOException, NotFoundException, ChecksumException, FormatException { byte[] decodedByte = Base64.getDecoder().decode(encodedQrCode); BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedByte)); BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Map<DecodeHintType, Object> hintMap = new HashMap<>(); hintMap.put(DecodeHintType.PURE_BARCODE, true); return reader.decode(bitmap, hintMap).getText(); }
private String qrCodeText(String dataUrl) throws Exception { QRCodeReader reader = new QRCodeReader(); String[] rawSplit = dataUrl.split(","); assertEquals("data:image/png;base64", rawSplit[0]); byte[] decodedByte = Base64.getDecoder().decode(rawSplit[1]); BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedByte)); BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); Map<DecodeHintType, Object> hintMap = new HashMap<>(); hintMap.put(DecodeHintType.PURE_BARCODE, true); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); return reader.decode(bitmap, hintMap).getText(); }
/** * 解码 * * @param filePath * @return */ @SuppressWarnings({"rawtypes", "unchecked"}) public static String decode(String filePath) throws IOException, NotFoundException { BufferedImage image = ImageIO.read(new File(filePath)); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Hashtable<DecodeHintType, Object> hints = new Hashtable<>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); }
/** * 二维码解码 * * @param imgPath 文件路径 * @return String string */ public static String decode2(String imgPath) { BufferedImage image; Result result; try { image = ImageIO.read(new File(imgPath)); if (image == null) { LOGGER.error("the decode image may be not exit."); return null; } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); EnumMap<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class); hints.put(DecodeHintType.CHARACTER_SET, "GBK"); result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } catch (Exception e) { LOGGER.error("二维码解码错误", e); } return null; }