/** * @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; } }
/** * 条形码解码 */ public static String decode(BufferedImage image) { Result result = null; try { if (image == null) { System.out.println("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; }
@Override public LuminanceSource rotateCounterClockwise() { int sourceWidth = this.image.getWidth(); int sourceHeight = this.image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics(); g.drawImage(this.image, transform, null); g.dispose(); int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, this.top, sourceWidth - (this.left + width), getHeight(), width); }
public static String decodeQr(String filePath) { String retStr = ""; if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) { return "图片路径为空!"; } try { BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath)); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap bitmap = new BinaryBitmap(binarizer); HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>(); hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap); retStr = result.getText(); } catch (Exception e) { logger.error("", e); } return retStr; }
public static String decodeQr(String filePath) { String retStr = ""; if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) { return "图片路径为空!"; } try { BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath)); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap bitmap = new BinaryBitmap(binarizer); HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>(); hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap); retStr = result.getText(); } catch (Exception e) { e.printStackTrace(); } return retStr; }
/** * 解析条形码 */ public static String decodes(String imgPath) { BufferedImage image = null; Result result = null; try { image = ImageIO.read(new File(imgPath)); if (image == null) { System.out.println("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 条形码解码 * * @param imgPath 文件路径 * @return String string */ public static String decode(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)); result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } catch (Exception e) { LOGGER.error("条形码解析错误", e); } return null; }
/** * 二维码解码 * * @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; }
/** * 读取二维码 * @param qrCodeFile * @return */ public String readQrCode(File qrCodeFile){ String ret = null; try { QRCodeReader reader = new QRCodeReader(); BufferedImage image = ImageIO.read(qrCodeFile); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap imageBinaryBitmap = new BinaryBitmap(binarizer); Result result = reader.decode(imageBinaryBitmap); ret = result.getText(); } catch (IOException |NotFoundException | ChecksumException | FormatException e) { Exceptions.printException(e); } return ret; }
/** * 条形码解码 * * @param imgPath * @return String */ public static String decode(String imgPath) { BufferedImage image = null; Result result = null; try { image = ImageIO.read(new File(imgPath)); if (image == null) { System.out.println("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 二维码解码 * * @param imgPath * @return String */ public static String decode2(String imgPath) { BufferedImage image = null; Result result = null; try { image = ImageIO.read(new File(imgPath)); if (image == null) { System.out.println("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "GBK"); result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Decodes a QR code from a BufferedImage object. * * @param image * @return a Result object containing the decoded data or information about * an unsuccessful decoding attempt * @throws Exception */ private Result decode(BufferedImage image) throws Exception { // create a luminance source from the BufferedImage LuminanceSource lumSource = new BufferedImageLuminanceSource(image); // create a binary bitmap from the luminance source. a Binarizer // converts luminance data to 1 bit data. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource)); // a reader for decoding QRCodeReader reader = new QRCodeReader(); // attempt decoding and return result Hashtable<DecodeHintType, Boolean> hints = new Hashtable<DecodeHintType, Boolean>(); hints.put(DecodeHintType.TRY_HARDER, true); return reader.decode(bitmap, hints); }
@Override public LuminanceSource rotateCounterClockwise() { //if (!isRotateSupported()) { // throw new IllegalStateException("Rotate not supported"); //} int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); // Rotate 90 degrees counterclockwise. AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); // Note width/height are flipped since we are rotating 90 degrees. BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); // Draw the original image into rotated, via transformation Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); // Maintain the cropped region, but rotate it too. int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width); }
@Override public LuminanceSource rotateCounterClockwise() { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); // Rotate 90 degrees counterclockwise. AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); // Note width/height are flipped since we are rotating 90 degrees. BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); // Draw the original image into rotated, via transformation Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); // Maintain the cropped region, but rotate it too. int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width); }
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; if ((width & 0x07) != 0) { subWidth++; } int subHeight = height >> 3; if ((height & 0x07) != 0) { subHeight++; } int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); matrix = new BitMatrix(width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, matrix); } else { // If the image is too small, fall back to the global histogram approach. matrix = super.getBlackMatrix(); } } }
@Ignore public void testMulti() throws Exception { // Very basic test for now Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-1"); Path testImage = testBase.resolve("1.png"); BufferedImage image = ImageIO.read(testImage.toFile()); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(new MultiFormatReader()); Result[] results = reader.decodeMultiple(bitmap); assertNotNull(results); assertEquals(2, results.length); assertEquals("031415926531", results[0].getText()); assertEquals(BarcodeFormat.UPC_A, results[0].getBarcodeFormat()); assertEquals("www.airtable.com/jobs", results[1].getText()); assertEquals(BarcodeFormat.QR_CODE, results[1].getBarcodeFormat()); }
@Override public void onPreviewFrame(byte[] data, Camera camera) { // TODO Auto-generated method stub if (mDialog.isShowing()) return; LuminanceSource source = new PlanarYUVLuminanceSource(data, mWidth, mHeight, mLeft, mTop, mAreaWidth, mAreaHeight, false); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer( source)); Result result; try { result = mMultiFormatReader.decode(bitmap, null); if (result != null) { mDialog.setTitle("Result"); mDialog.setMessage(result.getText()); mDialog.show(); } } catch (NotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Try decoding standard QR code without any of the * sequence information inserted into payload. */ @Test public void testDecodeQrWithNoSequenceInfo() { // Decode qr code received from transmitter as screenshot String filename = "fooScreenshot_noReservedBits.png"; String expectedText = "foo"; BufferedImage b = getImageResourceAndCheckNotNull(filename); LuminanceSource lumSrc = new BufferedImageLuminanceSource(b); assertNotNull("Unable to convert BufferedImage to LuminanceSrc", lumSrc); try { Result result = Receive.decodeSingle(lumSrc); assertEquals("Expect decoded result to match expected", expectedText, result.getText()); } catch (NotFoundException e) { fail("Unable to find QR in image, "+filename + ". " + e.getMessage()); } }
private Iterable<Result> decodeMultipleAndCheckValidQR(LuminanceSource img, String filename) { Iterable<Result> results = null; try { results = Receive.decodeMultiple(img); assertNotNull("QR result should not be null", results); Iterator<Result> resIter = results.iterator(); assertTrue("QR has at least one result", resIter.hasNext()); } catch (NotFoundException e) { if (filename != null) { fail("Unable to find QR in image, " + filename +". "+ e.getMessage()); }else{ fail("Unable to find QR in image. " + e.getMessage()); } } return results; }
@Test public void testDecodeTwoQRCodesSameImage() throws ReceiveException { String expectAll = "qr_4Of4.png"; // Use receive to decode this qr code: Receive receive = new Receive(0, 0, NULL_MONITOR); // The received data and track transmission status. DecodedMessage message = new DecodedMessage(NULL_MONITOR); LuminanceSource img = getLuminanceImgAndCheckNotNull(expectAll); Iterable <Result> results = decodeMultipleAndCheckValidQR(img,expectAll); State s = receive.saveMessageAndUpdateProgress(results, message); assertTrue("Expected to be in Final state.", s == State.Final); System.out.println("MESSAGE=" + message.toString()); }
public String decode(BufferedImage image) { // convert the image to a binary bitmap source LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); // decode the barcode QRCodeReader reader = new QRCodeReader(); try { @SuppressWarnings("rawtypes") Hashtable hints = new Hashtable(); Result result = reader.decode(bitmap, hints); log.info("Decoded image successfully, result was : '" + result.getText() + "'"); return result.getText(); } catch (ReaderException e) { // the data is improperly formatted log.debug(e.getMessage()); log.error("Error while decoding image", e); } return ""; }
@Override public BitArray getBlackRow(int y, BitArray row) throws NotFoundException { LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); if (row == null || row.getSize() < width) { row = new BitArray(width); } else { row.clear(); } initArrays(width); byte[] localLuminances = source.getRow(y, luminances); int[] localBuckets = buckets; for (int x = 0; x < width; x++) { int pixel = localLuminances[x] & 0xff; localBuckets[pixel >> LUMINANCE_SHIFT]++; } int blackPoint = estimateBlackPoint(localBuckets); int left = localLuminances[0] & 0xff; int center = localLuminances[1] & 0xff; for (int x = 1; x < width - 1; x++) { int right = localLuminances[x + 1] & 0xff; // A simple -1 4 -1 box filter with a weight of 2. int luminance = ((center * 4) - left - right) / 2; if (luminance < blackPoint) { row.set(x); } left = center; center = right; } return row; }
/** * Calculates the final BitMatrix once for all requests. This could be called once from the * constructor instead, but there are some advantages to doing it lazily, such as making * profiling easier, and not doing heavy lifting when callers don't expect it. */ @Override public BitMatrix getBlackMatrix() throws NotFoundException { if (matrix != null) { return matrix; } LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); int height = source.getHeight(); if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) { byte[] luminances = source.getMatrix(); int subWidth = width >> BLOCK_SIZE_POWER; if ((width & BLOCK_SIZE_MASK) != 0) { subWidth++; } int subHeight = height >> BLOCK_SIZE_POWER; if ((height & BLOCK_SIZE_MASK) != 0) { subHeight++; } int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); BitMatrix newMatrix = new BitMatrix(width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix); matrix = newMatrix; } else { // If the image is too small, fall back to the global histogram approach. matrix = super.getBlackMatrix(); } return matrix; }
protected LuminanceSource createSource(SourceData sourceData) { if (this.cropRect == null) { return null; } else { return sourceData.createSource(); } }
@Override public BitArray getBlackRow(int y, BitArray row) throws NotFoundException { LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); if (row == null || row.getSize() < width) { row = new BitArray(width); } else { row.clear(); } initArrays(width); byte[] localLuminances = source.getRow(y, luminances); int[] localBuckets = buckets; for (int x = 0; x < width; x++) { localBuckets[(localLuminances[x] & 0xff) >> LUMINANCE_SHIFT]++; } int blackPoint = estimateBlackPoint(localBuckets); if (width < 3) { // Special case for very small images for (int x = 0; x < width; x++) { if ((localLuminances[x] & 0xff) < blackPoint) { row.set(x); } } } else { int left = localLuminances[0] & 0xff; int center = localLuminances[1] & 0xff; for (int x = 1; x < width - 1; x++) { int right = localLuminances[x + 1] & 0xff; // A simple -1 4 -1 box filter with a weight of 2. if (((center * 4) - left - right) / 2 < blackPoint) { row.set(x); } left = center; center = right; } } return row; }
public LuminanceSource rotateCounterClockwise() { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); int width = getWidth(); return new CodeImage(rotatedImage, top, sourceWidth - (left + width), getHeight(), width); }
public LuminanceSource rotateCounterClockwise() { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width); }
public BitArray getBlackRow(int y, BitArray row) throws NotFoundException { int x; LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); if (row == null || row.getSize() < width) { row = new BitArray(width); } else { row.clear(); } initArrays(width); byte[] localLuminances = source.getRow(y, this.luminances); int[] localBuckets = this.buckets; for (x = 0; x < width; x++) { int i = (localLuminances[x] & 255) >> 3; localBuckets[i] = localBuckets[i] + 1; } int blackPoint = estimateBlackPoint(localBuckets); int left = localLuminances[0] & 255; int center = localLuminances[1] & 255; for (x = 1; x < width - 1; x++) { int right = localLuminances[x + 1] & 255; if ((((center * 4) - left) - right) / 2 < blackPoint) { row.set(x); } left = center; center = right; } return row; }
public BitMatrix getBlackMatrix() throws NotFoundException { int y; byte[] localLuminances; LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); int height = source.getHeight(); BitMatrix matrix = new BitMatrix(width, height); initArrays(width); int[] localBuckets = this.buckets; for (y = 1; y < 5; y++) { int x; localLuminances = source.getRow((height * y) / 5, this.luminances); int right = (width * 4) / 5; for (x = width / 5; x < right; x++) { int i = (localLuminances[x] & 255) >> 3; localBuckets[i] = localBuckets[i] + 1; } } int blackPoint = estimateBlackPoint(localBuckets); localLuminances = source.getMatrix(); for (y = 0; y < height; y++) { int offset = y * width; for (x = 0; x < width; x++) { if ((localLuminances[offset + x] & 255) < blackPoint) { matrix.set(x, y); } } } return matrix; }
public BitMatrix getBlackMatrix() throws NotFoundException { if (this.matrix != null) { return this.matrix; } LuminanceSource source = getLuminanceSource(); int width = source.getWidth(); int height = source.getHeight(); if (width < 40 || height < 40) { this.matrix = super.getBlackMatrix(); } else { byte[] luminances = source.getMatrix(); int subWidth = width >> 3; if ((width & 7) != 0) { subWidth++; } int subHeight = height >> 3; if ((height & 7) != 0) { subHeight++; } int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); BitMatrix newMatrix = new BitMatrix(width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix); this.matrix = newMatrix; } return this.matrix; }
private static Bitmap toBitmap(LuminanceSource source, int[] pixels) { int width = source.getWidth(); int height = source.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
private Result decode(LuminanceSource source) { Result rawResult = null; if (source != null) { BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap); } catch (ReaderException re) { //continue } finally { multiFormatReader.reset(); } } return rawResult; }