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; }
/** * Searches Bitmap image for a QR code, and returns the String representation * of it if a valid QR code was found. * Returns empty String if no valid QR code was found. * * @param bitmap The Bitmap to decode * @return The string representation of the Bitmap, or "" if no valid QR code was found */ @Override protected String doInBackground(BinaryBitmap... bitmap) { String decodedText; // get QR reader final Reader reader = new QRCodeReader(); // try to decode QR code try { // get Result from decoder final Result result = reader.decode(bitmap[0]); // get text from Result decodedText = result.getText(); } catch (Exception e) { // set text to blank, no QR code found decodedText = ""; } // return text return decodedText; }
/** * 读取二维码 * @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; }
@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(); } } }); }
/** * 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()); } }
/** * 扫描二维码图片的方法 * * @param path * @return */ public Result scanningImage(String path) { try { if (TextUtils.isEmpty(path)) { return null; } Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 设置二维码内容的编码 InputStream is = new FileInputStream(path); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; // width,hight设为原来的十分一 options.inSampleSize = 10; scanBitmap = BitmapFactory.decodeStream(is, null, options); RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); return reader.decode(bitmap, hints); } 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); }
public static 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); return result.getText(); } catch (ReaderException e) { // the data is improperly formatted } return ""; }
private String decode(byte[] data, int width, int height) { ScannerManager manager = mManager.get(); if (manager == null) { return null; } Rect rect = manager.getFramingRectInPreview(); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.right, rect.bottom, false); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap, mHints); return result.getText(); } catch (ReaderException e) { // Ignore as we will repeatedly decode the preview frame return null; } }
/** * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This * is important for performance in continuous scan clients. * * @param hints The set of hints to use for subsequent calls to decode(image) */ public void setHints(Map<DecodeHintType,?> hints) { this.hints = hints; // boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); Collection<Reader> readers = new ArrayList<Reader>(); if (formats != null) { if (formats.contains(BarcodeFormat.QR_CODE)) { readers.add(new QRCodeReader()); } } if (readers.isEmpty()) { readers.add(new QRCodeReader()); } this.readers = readers.toArray(new Reader[readers.size()]); }
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { mCamera = getCameraInstance(); setCameraDisplayOrientation(); try { mReader = new QRCodeReader(); setPreviewParameters(mCamera); mCamera.setPreviewTexture(surface); mCamera.setPreviewCallback(mCameraPreviewCallback); mCamera.setErrorCallback(mCameraErrorCallback); mCamera.startPreview(); } catch (Exception e) { Log.e(TAG, "Failed to init preview: " + e.getLocalizedMessage()); } // mTextureView.setVisibility(View.INVISIBLE); // mTextureView.setAlpha(0.5f); webView.getView().setBackgroundColor(0x00000000); webView.getView().bringToFront(); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_decoder, container, false); mQrReader = new QRCodeReader(); mTextureView = new AutoFitTextureView(getActivity()); layout = (RelativeLayout) rootView.findViewById(R.id.fragment_decoder_layout); new Handler().postDelayed(new Runnable() { @Override public void run() { if (layout.getChildAt(0) == mTextureView) return; layout.addView(mTextureView, 0); startBackgroundThread(); } }, 700); return rootView; }
/** * Versucht aus dem �bergeben BufferedImage ein QR Code zu finden und �bersetzt dieses in einen String. * * @param qrcodeImage : BufferedImage * @return String mit dem Inhalt des QRCodes * @throws Exception */ private static String readQRCode(BufferedImage qrcodeImage) throws Exception{ //Die Parameter anlegen Hashtable<DecodeHintType, Object> hintMap = new Hashtable<DecodeHintType, Object>(); hintMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); //Bild zu BinaryBitmap verwandeln BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(qrcodeImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); //QR Leser initialisieren... QRCodeReader reader = new QRCodeReader(); Result result; //...und lesen: result = reader.decode(bitmap,hintMap); return result.getText(); }
public IAnswerData processImage(Image image) throws ImageProcessingException { MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource( image); Reader reader = new QRCodeReader(); Hashtable hints = new Hashtable(); // hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); try { Result result = reader.decode(source, hints); if ((result != null) && (result.getText() != null)) { String scannedCode = result.getText(); return new StringData(scannedCode); } else { throw new ImageProcessingException("Barcode scanning failed"); } } catch (ReaderException re) { throw new ImageProcessingException("Barcode scanning failed"); } }
private void scanQR(BufferedImage i) throws ReaderException, ChecksumException, FormatException { Image img = i.getScaledInstance(200, -1, Image.SCALE_SMOOTH); // Create a buffered image with transparency i = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Draw the image on to the buffered image Graphics2D bGr = i.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); btnDragOrPaste.setIcon(new ImageIcon(i)); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(i))); //Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2); //hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); //Vector decodeFormats = new Vector<BarcodeFormat>(); //decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); //Hashtable hints = new Hashtable<DecodeHintType, Object>(3); //hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); Result result = new QRCodeReader().decode(binaryBitmap);//, hints); //System.out.println("QR Code : "+result.getText()); textField.setText(result.getText()); //System.out.println( s ); }
private void scanQR(BufferedImage i) throws ReaderException, ChecksumException, FormatException { Image img = i.getScaledInstance(200, -1, Image.SCALE_SMOOTH); // Create a buffered image with transparency i = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Draw the image on to the buffered image Graphics2D bGr = i.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); setIcon(new ImageIcon(i)); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(i))); //Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2); //hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); //Vector decodeFormats = new Vector<BarcodeFormat>(); //decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); //Hashtable hints = new Hashtable<DecodeHintType, Object>(3); //hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); Result result = new QRCodeReader().decode(binaryBitmap);//, hints); //System.out.println("QR Code : "+result.getText()); scannedQRText = result.getText(); fireEvent(); //System.out.println( s ); }
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 void onPreviewFrame(byte[] data, Camera camera) { int previewWidth = camera.getParameters().getPreviewSize().width; int previewHeight = camera.getParameters().getPreviewSize().height; PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource( data, previewWidth, previewHeight, 0, 0, previewWidth, previewHeight, false); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap); String text = result.getText(); Intent intent = new Intent(); intent.setData(Uri.parse(text)); setResult(RESULT_OK, intent); finish(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_SHORT).show(); } }
/** * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader * objects from one decode to the next. */ public static Result decodeImage(byte[] data, int width, int height) { // 处理 Result result = null; try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false); /** * HybridBinarizer算法使用了更高级的算法,但使用GlobalHistogramBinarizer识别效率确实比HybridBinarizer要高一些。 * * GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/) * * 二值化的关键就是定义出黑白的界限,我们的图像已经转化为了灰度图像,每个点都是由一个灰度值来表示,就需要定义出一个灰度值,大于这个值就为白(0),低于这个值就为黑(1)。 * 在GlobalHistogramBinarizer中,是从图像中均匀取5行(覆盖整个图像高度),每行取中间五分之四作为样本;以灰度值为X轴,每个灰度值的像素个数为Y轴建立一个直方图, * 从直方图中取点数最多的一个灰度值,然后再去给其他的灰度值进行分数计算,按照点数乘以与最多点数灰度值的距离的平方来进行打分,选分数最高的一个灰度值。接下来在这两个灰度值中间选取一个区分界限, * 取的原则是尽量靠近中间并且要点数越少越好。界限有了以后就容易了,与整幅图像的每个点进行比较,如果灰度值比界限小的就是黑,在新的矩阵中将该点置1,其余的就是白,为0。 */ BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source)); // BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2 = new QRCodeReader(); result = reader2.decode(bitmap1, hints); } catch (ReaderException e) { } return result; }
private static void parse() throws IOException, NotFoundException, ChecksumException, FormatException { BufferedImage image = ImageReader.readImage(Paths.get("d:/qr.png").toUri()); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer bin = new HybridBinarizer(source); BinaryBitmap bitmap = new BinaryBitmap(bin); Result result = new QRCodeReader().decode(bitmap); System.out.println(result.toString()); }
private static Observable<Result> resolve(LuminanceSource source, boolean failWhenNotFound) { BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { return Observable.just(reader.decode(bitmap, TRY_HARDER)); } catch (NotFoundException | ChecksumException | FormatException e) { if (failWhenNotFound) { return Observable.error(e); } } finally { reader.reset(); } return Observable.empty(); }
protected Result scanningImage(String path) { if (StringUtil.isBlank(path)) { return null; } // DecodeHintType 和EncodeHintType Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 先获取原大小 scanBitmap = BitmapFactory.decodeFile(path, options); options.inJustDecodeBounds = false; // 获取新的大小 int sampleSize = (int) (options.outHeight / (float) 200); if (sampleSize <= 0) sampleSize = 1; options.inSampleSize = sampleSize; scanBitmap = BitmapFactory.decodeFile(path, options); int width = scanBitmap.getWidth(), height = scanBitmap.getHeight(); int[] pixels = new int[width * height]; scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height); scanBitmap.recycle(); scanBitmap = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { return reader.decode(bitmap1, hints); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 解码 QRCode 图片,解析出其内容 * * @param imageURI QRCode 图片 URI * @return 解析后的内容 * @throws IOException */ public static String decodeQRCodeImage(URI imageURI) throws IOException { BufferedImage bufferedImage = ImageReader.readImage(imageURI); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap); return result.getText(); } catch (ReaderException e) { e.printStackTrace(); } return ""; }
public static String getStringFromQRCode(String path) { String httpString = null; Bitmap bmp = convertToBitmap(path); byte[] data = getYUV420sp(bmp.getWidth(), bmp.getHeight(), bmp); // 处理 try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); // hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, bmp.getWidth(), bmp.getHeight(), 0, 0, bmp.getWidth(), bmp.getHeight(), false); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2= new QRCodeReader(); Result result = reader2.decode(bitmap1, hints); httpString = result.getText(); } catch (Exception e) { e.printStackTrace(); } bmp.recycle(); bmp = null; return httpString; }
public static Result decodePureBitmap(Bitmap bmp) throws FormatException, ChecksumException, NotFoundException { int width = bmp.getWidth(), height = bmp.getHeight(); int[] pixels = new int[width * height]; bmp.getPixels(pixels, 0, width, 0, 0, width, height); RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); QRCodeReader reader = new QRCodeReader(); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class); hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); return reader.decode(binaryBitmap, hints); }
/** * Returns a ZXing reader that can read the specified symbol. * * @param symbol the symbol to be read * @return a ZXing reader that can read the specified symbol */ private static Reader findReader(Symbol symbol) { if (symbol instanceof Code128 || symbol instanceof UspsPackage) { return new Code128Reader(); } else if (symbol instanceof Code93) { return new Code93Reader(); } else if (symbol instanceof Code3Of9) { return new Code39Reader(); } else if (symbol instanceof Codabar) { return new CodaBarReader(); } else if (symbol instanceof AztecCode) { return new AztecReader(); } else if (symbol instanceof QrCode) { return new QRCodeReader(); } else if (symbol instanceof Ean) { Ean ean = (Ean) symbol; if (ean.getMode() == Ean.Mode.EAN8) { return new EAN8Reader(); } else { return new EAN13Reader(); } } else if (symbol instanceof Pdf417) { Pdf417 pdf417 = (Pdf417) symbol; if (pdf417.getMode() != Pdf417.Mode.MICRO) { return new PDF417Reader(); } } else if (symbol instanceof Upc) { Upc upc = (Upc) symbol; if (upc.getMode() == Upc.Mode.UPCA) { return new UPCAReader(); } else { return new UPCEReader(); } } // no corresponding ZXing reader exists, or it behaves badly so we don't use it for testing return null; }
/** * 读取二维码 * @param file 二维码源 * @throws Exception */ private static void readCode(File file) throws Exception{ BufferedImage encodedBufferedImage = ImageIO.read(file) ; LuminanceSource source = new BufferedImageLuminanceSource(encodedBufferedImage); Result result = new QRCodeReader().decode(new BinaryBitmap(new HybridBinarizer(source))); System.out.println(result.getText()); }
/** * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This * is important for performance in continuous scan clients. * * @param hints The set of hints to use for subsequent calls to decode(image) */ public void setHints(Map<DecodeHintType,?> hints) { this.hints = hints; boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); Collection<Reader> readers = new ArrayList<Reader>(); if (formats != null) { boolean addOneDReader = formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_E) || formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.CODABAR) || formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.ITF) || formats.contains(BarcodeFormat.RSS_14) || formats.contains(BarcodeFormat.RSS_EXPANDED); // Put 1D readers upfront in "normal" mode if (addOneDReader && !tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } if (formats.contains(BarcodeFormat.QR_CODE)) { readers.add(new QRCodeReader()); } if (formats.contains(BarcodeFormat.DATA_MATRIX)) { readers.add(new DataMatrixReader()); } if (formats.contains(BarcodeFormat.AZTEC)) { readers.add(new AztecReader()); } if (formats.contains(BarcodeFormat.PDF_417)) { readers.add(new PDF417Reader()); } if (formats.contains(BarcodeFormat.MAXICODE)) { readers.add(new MaxiCodeReader()); } // At end in "try harder" mode if (addOneDReader && tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } if (readers.isEmpty()) { if (!tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } readers.add(new QRCodeReader()); readers.add(new DataMatrixReader()); readers.add(new AztecReader()); readers.add(new PDF417Reader()); readers.add(new MaxiCodeReader()); if (tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } this.readers = readers.toArray(new Reader[readers.size()]); }
/** * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This * is important for performance in continuous scan clients. * * @param hints The set of hints to use for subsequent calls to decode(image) */ public void setHints(Map<DecodeHintType,?> hints) { this.hints = hints; boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); Collection<Reader> readers = new ArrayList<>(); if (formats != null) { boolean addOneDReader = formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_E) || formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.CODABAR) || formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.ITF) || formats.contains(BarcodeFormat.RSS_14) || formats.contains(BarcodeFormat.RSS_EXPANDED); // Put 1D readers upfront in "normal" mode if (addOneDReader && !tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } if (formats.contains(BarcodeFormat.QR_CODE)) { readers.add(new QRCodeReader()); } // if (formats.contains(BarcodeFormat.DATA_MATRIX)) { // readers.add(new DataMatrixReader()); // } // if (formats.contains(BarcodeFormat.AZTEC)) { // readers.add(new AztecReader()); // } // if (formats.contains(BarcodeFormat.PDF_417)) { // readers.add(new PDF417Reader()); // } // if (formats.contains(BarcodeFormat.MAXICODE)) { // readers.add(new MaxiCodeReader()); // } // // At end in "try harder" mode if (addOneDReader && tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } if (readers.isEmpty()) { if (!tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } readers.add(new QRCodeReader()); // readers.add(new DataMatrixReader()); // readers.add(new AztecReader()); // readers.add(new PDF417Reader()); // readers.add(new MaxiCodeReader()); if (tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } this.readers = readers.toArray(new Reader[readers.size()]); }
/** * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This * is important for performance in continuous scan clients. * * @param hints The set of hints to use for subsequent calls to decode(image) */ public void setHints(Map<DecodeHintType,?> hints) { this.hints = hints; boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); Collection<Reader> readers = new ArrayList<>(); if (formats != null) { boolean addOneDReader = formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_E) || formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.CODABAR) || formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.ITF) || formats.contains(BarcodeFormat.RSS_14) || formats.contains(BarcodeFormat.RSS_EXPANDED); // Put 1D readers upfront in "normal" mode if (addOneDReader && !tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } if (formats.contains(BarcodeFormat.QR_CODE)) { readers.add(new QRCodeReader()); } // At end in "try harder" mode if (addOneDReader && tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } if (readers.isEmpty()) { if (!tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } readers.add(new QRCodeReader()); if (tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } this.readers = readers.toArray(new Reader[readers.size()]); }
private String decode(byte[] imageBytes) throws IOException, NotFoundException, ChecksumException, FormatException { BinaryBitmap bitmap = readToBitmap(imageBytes); return new QRCodeReader().decode(bitmap).getText(); }
/** * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This * is important for performance in continuous scan clients. * * @param hints The set of hints to use for subsequent calls to decode(image) */ public void setHints(Map<DecodeHintType,?> hints) { this.hints = hints; boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); @SuppressWarnings("unchecked") Collection<BarcodeFormat> formats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); Collection<Reader> readers = new ArrayList<>(); if (formats != null) { boolean addOneDReader = formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_E) || formats.contains(BarcodeFormat.EAN_13) || formats.contains(BarcodeFormat.EAN_8) || formats.contains(BarcodeFormat.CODABAR) || formats.contains(BarcodeFormat.CODE_39) || formats.contains(BarcodeFormat.CODE_93) || formats.contains(BarcodeFormat.CODE_128) || formats.contains(BarcodeFormat.ITF) || formats.contains(BarcodeFormat.RSS_14) || formats.contains(BarcodeFormat.RSS_EXPANDED); // Put 1D readers upfront in "normal" mode if (addOneDReader && !tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } if (formats.contains(BarcodeFormat.QR_CODE)) { readers.add(new QRCodeReader()); } if (formats.contains(BarcodeFormat.DATA_MATRIX)) { readers.add(new DataMatrixReader()); } if (formats.contains(BarcodeFormat.AZTEC)) { readers.add(new AztecReader()); } if (formats.contains(BarcodeFormat.PDF_417)) { readers.add(new PDF417Reader()); } if (formats.contains(BarcodeFormat.MAXICODE)) { readers.add(new MaxiCodeReader()); } // At end in "try harder" mode if (addOneDReader && tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } if (readers.isEmpty()) { if (!tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } readers.add(new QRCodeReader()); readers.add(new DataMatrixReader()); readers.add(new AztecReader()); readers.add(new PDF417Reader()); readers.add(new MaxiCodeReader()); if (tryHarder) { readers.add(new MultiFormatOneDReader(hints)); } } this.readers = readers.toArray(new Reader[readers.size()]); }
@Override protected Result doInBackground(Uri... params) { if (params == null || params.length != 1) { return null; } try { InputStream inputStream = QRScanActivity.this.getContentResolver().openInputStream(params[0]); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Rect padding = new Rect(0, 0, 0, 0); BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); int minLength = Math.min(options.outWidth, options.outHeight); int MAX = 512; // 图片短边不超过 512 if (minLength > MAX) { options.inSampleSize = minLength / MAX; } options.inJustDecodeBounds = false; // 流打开后只能用一次, 需要重新获取 inputStream = QRScanActivity.this.getContentResolver().openInputStream(params[0]); // 对图片裁剪后再扫码, 否则花的时间太长 Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options); if (bitmap == null) { return null; } int width = bitmap.getWidth(), height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); bitmap.recycle(); RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Result result = reader.decode(bBitmap); return result; } catch (Exception e) { Log.e(TAG, "can not open file", e); return null; } }
public static Observable<Result> scanFromPicture(String path) { return Observable.fromCallable(() -> { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, QR_CODE_IN_SAMPLE_LENGTH, QR_CODE_IN_SAMPLE_LENGTH); options.inJustDecodeBounds = false; int[] pixels = null; QRCodeReader reader = new QRCodeReader(); int retryTimes = 0; Result result = null; while (result == null && retryTimes < MAX_RETRY_TIMES) { Bitmap picture = BitmapFactory.decodeFile(path, options); int width = picture.getWidth(); int height = picture.getHeight(); if (pixels == null) { pixels = new int[picture.getWidth() * picture.getHeight()]; } picture.getPixels(pixels, 0, width, 0, 0, width, height); picture.recycle(); LuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { result = reader.decode(bitmap, TRY_HARDER); } catch (NotFoundException | ChecksumException | FormatException ignored) { retryTimes++; options.inSampleSize *= 2; } } reader.reset(); return result; }).flatMap(result -> { if (result == null) { return Observable.error(NotFoundException.getNotFoundInstance()); } return Observable.just(result); }); }