/** * @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; }
/** * 解析二维码 * * @param file 二维码图片 * @return * @throws Exception */ public static String decode(File file) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
/** * 获取解码结果 * * @param bitmap * @return */ public Result getRawResult(Bitmap bitmap) { if (bitmap == null) { return null; } try { return multiFormatReader.decodeWithState(new BinaryBitmap( new HybridBinarizer(new BitmapLuminanceSource(bitmap)))); } catch (NotFoundException e) { e.printStackTrace(); } return null; }
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; }
/** * 同步解析bitmap二维码。该方法是耗时操作,请在子线程中调用。 * * @param bitmap 要解析的二维码图片 * @return 返回二维码图片里的内容 或 null */ public static String syncDecodeQRCode(Bitmap bitmap) { try { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); Result result = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS); return result.getText(); } catch (Exception e) { return null; } }
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 String decodeWithZxing(byte[] data, int width, int height, Rect crop) { MultiFormatReader multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(changeZXingDecodeDataMode()); Result rawResult = null; PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, crop.left, crop.top, crop.width(), crop.height(), false); if (source != null) { BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); } } return rawResult != null ? rawResult.getText() : null; }
public String decodeWithZxing(Bitmap bitmap) { MultiFormatReader multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(changeZXingDecodeDataMode()); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); Result rawResult = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); if (source != null) { BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(binaryBitmap); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); } } return rawResult != null ? rawResult.getText() : null; }
/** * 解析二维码图片工具类 * * @param bitmap */ public static String analyzeBitmap(Bitmap bitmap) { MultiFormatReader multiFormatReader = new MultiFormatReader(); // 解码的参数 Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2); // 可以解析的编码类型 Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>(); if (decodeFormats == null || decodeFormats.isEmpty()) { decodeFormats = new Vector<BarcodeFormat>(); // 这里设置可扫描的类型,我这里选择了都支持 decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS); decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS); } hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); // 设置继续的字符编码格式为UTF8 hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 设置解析配置参数 multiFormatReader.setHints(hints); // 开始对图像资源解码 Result rawResult = null; try { rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap)))); } catch (Exception e) { e.printStackTrace(); } if (rawResult != null) { return rawResult.getText(); } else { return "Failed"; } }
protected Result scanningImage(Uri path) { if (path == null || path.equals("")) { return null; } // DecodeHintType 和EncodeHintType Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 try { Bitmap scanBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); return reader.decode(bitmap1, hints); } catch (Exception e) { e.printStackTrace(); } return null; }
public static String decode(File file) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } CodeImage source = new CodeImage( image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
/** * 解析条形码 */ 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; }
public static String decode(File file) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource( image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
/** * 条形码解码 * * @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; }
public static Result decodeImage(final String path) { Bitmap bitmap = QrUtils.decodeSampledBitmapFromFile(path, 256, 256); // Google Photo 相册中选取云照片是会出现 Bitmap == null if (bitmap == null) return null; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); // RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); PlanarYUVLuminanceSource source1 = new PlanarYUVLuminanceSource(getYUV420sp(width, height, bitmap), width, height, 0, 0, width, height, false); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source1)); // BinaryBitmap binaryBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source1)); HashMap<DecodeHintType, Object> hints = new HashMap<>(); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); try { return new MultiFormatReader().decode(binaryBitmap, hints); } catch (NotFoundException e) { e.printStackTrace(); } return null; }
/** * Called as preview frames are displayed. This callback is invoked * on the event thread open(int) was called from. * <p/> * <p>If using the {@link ImageFormat#YV12} format, * refer to the equations in {@link Camera.Parameters#setPreviewFormat} * for the arrangement of the pixel data in the preview callback * buffers. * * @param data the contents of the preview frame in the format defined * by {@link ImageFormat}, which can be queried * with {@link Camera.Parameters#getPreviewFormat()}. * If {@link Camera.Parameters#setPreviewFormat(int)} * is never called, the default will be the YCbCr_420_SP * (NV21) format. * @param camera the Camera service object. */ @Override public void onPreviewFrame(byte[] data, Camera camera) { // get the size of the picture int frameHeight = camera.getParameters().getPreviewSize().height; int frameWidth = camera.getParameters().getPreviewSize().width; // create new Luminance Source final PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight); // convert to binary bitmap which can be used by our image decoder final BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); // set self as listener, and start decode image DecodeImageTask decodeImageTask = new DecodeImageTask(); decodeImageTask.setCallbackListener(this); decodeImageTask.execute(binaryBitmap); }
public Result decodeWithZxing(Bitmap bitmap) { MultiFormatReader multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(changeZXingDecodeDataMode()); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); Result rawResult = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(binaryBitmap); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); } return rawResult; }
/** * 读取二维码 * @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 file * 二维码图片 * @return * @throws Exception */ public static String decode(File file) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
/** * 条形码解码 * * @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; }
@Override public void processFrame(final Frame frame) { MAIN_THREAD_HANDLER.post(new Runnable() { @Override public void run() { try { reader = new QRCodeReader(); LuminanceSource ls = new PlanarYUVLuminanceSource( frame.image, frame.size.width, frame.size.height, 0, 0, frame.size.width, frame.size.height, false); Result r = reader.decode(new BinaryBitmap(new HybridBinarizer(ls))); sendTextToActivity(r.getText()); } catch (Exception e) { e.printStackTrace(); } } }); }
/** * Decode all barcodes in the image */ private String multiple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException { long begin = System.currentTimeMillis(); LuminanceSource source = new BufferedImageLuminanceSource(img); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); com.google.zxing.Reader reader = new MultiFormatReader(); MultipleBarcodeReader bcReader = new GenericMultipleBarcodeReader(reader); Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); StringBuilder sb = new StringBuilder(); for (Result result : bcReader.decodeMultiple(bitmap, hints)) { sb.append(result.getText()).append(" "); } SystemProfiling.log(null, System.currentTimeMillis() - begin); log.trace("multiple.Time: {}", System.currentTimeMillis() - begin); return sb.toString(); }
public static Result handleQRCodeFormBitmap(Bitmap bitmap) { RGBLuminanceSource source = new RGBLuminanceSource(bitmap); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); MultiFormatReader multiFormatReader = new MultiFormatReader(); Hashtable<DecodeHintType, Object> hints = new Hashtable<>(2); Vector<BarcodeFormat> decodeFormats = new Vector<>(); decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS); decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS); hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); multiFormatReader.setHints(hints); Result result = null; try { result = multiFormatReader.decodeWithState(binaryBitmap); } catch (NotFoundException e) { LogHelper.e(e); e.printStackTrace(); } return result; }
@Override protected Result doInBackground(Void... params) { BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource)); Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, listener); MultiFormatReader multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(hints); long start = System.currentTimeMillis(); Result rawResult = null; try { rawResult = multiFormatReader.decodeWithState(bitmap); mBitmap = luminanceSource.renderCroppedGreyScaleBitmap(); long end = System.currentTimeMillis(); Log.d("DecodeThread", "Decode use " + (end - start) + "ms"); } catch (ReaderException re) { } finally { multiFormatReader.reset(); } return rawResult; }
/** * 从二维码中,解析数据 * @param file 二维码图片文件 * @return 返回从二维码中解析到的数据值 * @throws Exception */ public static String decode(File file) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
/** * @desc 解析二维码 * @date 2015年10月10日-下午4:19:01 * @param file 待解析的二维码文件 * @param charsetName 编码UTF-8 * @return String 二维码内容 * @throws Exception */ public static String decode(File file,String charsetName) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource( image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, charsetName); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; }
/** * @param srcImgFilePath 要解码的图片地址 * @return */ public 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 hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); result = new MultiFormatReader().decode(bitmap, hints); } else { log.debug("Could not decode image."); } } catch (Throwable t) { log.error("decode-ex:{}", t); throw Throwables.propagate(t); } finally { return result; } }
/** * Writes decoded message to Decoded.txt * @param f Input QRCode image */ public void decode_qr(String f) { try { tv= (TextView)findViewById(R.id.dqr); tv.setText(""); Bitmap bmp=BitmapFactory.decodeFile(f); //import QRCode image file int width = bmp.getWidth(), height = bmp.getHeight(); int[] pixels = new int[width * height]; bmp.getPixels(pixels, 0, width, 0, 0, width, height); bmp.recycle(); bmp = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result qr_result = new QRCodeReader().decode(bitmap); tv.setText("Successfully Decoded!\n"); tv.append("Decoded file is at:\n"); write_to_file(qr_result.getText().toString()); } catch(Exception e) { Log.create_log(e, getApplicationContext()); } }