/** * 同步解析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 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; }
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; }
/** * 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()); } }
public String decode(final Bitmap image){ final long start = System.currentTimeMillis(); final int width = image.getWidth(), height = image.getHeight(); final int[] pixels = new int[width * height]; image.getPixels(pixels, 0, width, 0, 0, width, height); final RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { Result rawResult = mMultiFormatReader.decodeWithState(bitmap); final long end = System.currentTimeMillis(); Log.d(TAG, "QRCode decode in " + (end - start) + "ms"); Log.d(TAG, rawResult.toString()); return rawResult.getText(); } catch (NotFoundException re) { Log.w(TAG, re); return null; }finally { mMultiFormatReader.reset(); } }
public static String convert(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 rgbLuminanceSource = new RGBLuminanceSource( width, height, pixels); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer( rgbLuminanceSource)); HashMap<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET); MultiFormatReader multiFormatReader = new MultiFormatReader(); Result result = multiFormatReader.decode(binaryBitmap, hints); return result.getText(); } catch (Exception e) { return null; } }
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; }
public Result scanBitmap(Bitmap bitmap) throws FormatException, ChecksumException, NotFoundException { Reader reader = new MultiFormatReader(); Result result; int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()]; //copy pixel data from the Bitmap into the 'intArray' array bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray); BinaryBitmap bMap = new BinaryBitmap(new HybridBinarizer(source)); result = reader.decode(bMap); return result; }
public Result[] zxing( Mat inputImage ) throws ChecksumException, FormatException { int w = inputImage.width(); int h = inputImage.height(); Mat southEast; if (mBugRotate) { southEast = inputImage.submat(h-h/4 , h , 0 , w/2 - h/4 ); } else { southEast = inputImage.submat(0, h / 4, w / 2 + h / 4, w); } Bitmap bMap = Bitmap.createBitmap(southEast.width(), southEast.height(), Bitmap.Config.ARGB_8888); org.opencv.android.Utils.matToBitmap(southEast, bMap); southEast.release(); int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; //copy pixel data from the Bitmap into the 'intArray' array bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result[] results = {}; try { results = qrCodeMultiReader.decodeMultiple(bitmap); } catch (NotFoundException ignored) { } return results; }
private static RGBLuminanceSource createLuminanceSource(InputStream inputStream) { Bitmap bitmap = BitmapFactory.decodeStream(inputStream); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); bitmap.recycle(); return new RGBLuminanceSource(width, height, pixels); }
@Override public Bitmap processImage(Bitmap bMap) { int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; //copy pixel data from the Bitmap into the 'intArray' array bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; try { result = new MultiFormatReader().decode(bitmap); lastResult = result.getText(); lastFoundTime = System.currentTimeMillis(); fire(result.getText()); } catch (NotFoundException e) { //that's ok if (!lastResult.equals("")) //if result was not empty, clear old result { long mt1 = System.currentTimeMillis(); if (mt1 - lastFoundTime > CLEAR_RESULT_TIMEOUT) { lastFoundTime = mt1; fire(""); lastResult = ""; } } } return bMap; }
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); }
@Override public void handleMessage(Message message) { if (running) { switch (message.what) { case R.id.decode: RenderableLuminanceSource source = null; byte[] data = (byte[]) message.obj; if (data != null) { source = activity.getCameraManager().buildLuminanceSource(data); } decode(source); break; case R.id.decode_image: Bitmap bitmap = (Bitmap) message.obj; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); decodeLocalImage(new RGBLuminanceSource(width, height, pixels)); break; case R.id.quit: running = false; enhancedDecodeExecutor.shutdown(); Looper.myLooper().quit(); break; } } }
private Bitmap binarization(Bitmap bitmap, int lowColor, int highColor) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int pixels[] = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); LuminanceSource source = new RGBLuminanceSource(width, height, pixels); Binarizer binarizer = new HybridBinarizer(source); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); try { BitMatrix matrix = binarizer.getBlackMatrix(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (matrix.get(j, i)) { result.setPixel(j, i, highColor); } else { result.setPixel(j, i, lowColor); } } } } catch (NotFoundException e) { e.printStackTrace(); } return result; }
/** * 二值化 * * @param bitmap * @return */ private Bitmap binarization(Bitmap bitmap, int lowColor, int highStartColor, int highEndColor) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int pixels[] = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); LuminanceSource source = new RGBLuminanceSource(width, height, pixels); Binarizer binarizer = new HybridBinarizer(source); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); try { BitMatrix matrix = binarizer.getBlackMatrix(); int highColor; for (int i = 0; i < height; i++) { // highColor = getGradientColor(highStartColor, highEndColor, i / (float) height); highColor = getGradientColorByCurve(highStartColor, highEndColor, 0, height, i); for (int j = 0; j < width; j++) { if (matrix.get(j, i)) { result.setPixel(j, i, highColor); } else { result.setPixel(j, i, lowColor); } } } } catch (NotFoundException e) { e.printStackTrace(); } return result; }
@Override public String decode(int[] pixels, int width, int height) throws IOException { LuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; try { result = new MultiFormatReader().decode(bitmap, hints); } catch (NotFoundException e) { throw new IOException(e); } return result.getText(); }
@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); }); }
@ReactMethod public void decode(String path, Callback callback) { Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 先获取原大小 options.inJustDecodeBounds = false; // 获取新的大小 int sampleSize = (int) (options.outHeight / (float) 200); if (sampleSize <= 0) sampleSize = 1; options.inSampleSize = sampleSize; Bitmap scanBitmap = null; if (path.startsWith("http://")||path.startsWith("https://")) { scanBitmap = this.getbitmap(path); } else { scanBitmap = BitmapFactory.decodeFile(path, options); } if (scanBitmap == null) { callback.invoke("cannot load image"); return; } int[] intArray = new int[scanBitmap.getWidth()*scanBitmap.getHeight()]; scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight()); RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = reader.decode(bitmap, hints); if (result == null) { callback.invoke("image format error"); } else { callback.invoke(null, result.toString()); } } catch (Exception e) { callback.invoke("decode error"); } }
private static BinaryBitmap createBinaryBitmap(InputStream inputStream) { RGBLuminanceSource luminanceSource = createLuminanceSource(inputStream); HybridBinarizer hybridBinarizer = new HybridBinarizer(luminanceSource); return new BinaryBitmap(hybridBinarizer); }
@SuppressLint("StaticFieldLeak") private void scanImage(final Uri uri) { if (progressDialog != null) return; progressDialog = new ProgressDialogBuilder(this) .setTitle(R.string.please_wait) .setMessage(R.string.qr_scan_processing) .setCancelable(false) .show(); new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { UniFile file = UniFile.fromUri(QRCodeScanActivity.this, uri); if (file == null) return null; // ZXing can't process large image Bitmap bitmap = BitmapUtils.decodeStream(new UniFileInputStreamPipe(file), 1024, 1024); 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); bitmap.recycle(); RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); final HybridBinarizer hybBin = new HybridBinarizer(source); final BinaryBitmap bBitmap = new BinaryBitmap(hybBin); QRCodeReader reader = new QRCodeReader(); Map<DecodeHintType, Boolean> hints = new HashMap<>(); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); try { return reader.decode(bBitmap, hints).getText(); } catch (NotFoundException | FormatException | ChecksumException e) { // Try PURE_BARCODE hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); reader.reset(); try { return reader.decode(bBitmap, hints).getText(); } catch (NotFoundException | FormatException | ChecksumException ee) { return null; } } } @Override protected void onPostExecute(String text) { if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } if (text != null) { processCookieText(text); } else { Toast.makeText(QRCodeScanActivity.this, R.string.qr_scan_invalid, Toast.LENGTH_SHORT).show(); } } }.execute(); }
public static LuminanceSource getLuminanceSource(int imageFormat, byte[] data, int width, int height) { switch (imageFormat) { case ImageFormat.UNKNOWN: return null; case ImageFormat.NV21: // Source: http://www.programcreek.com/java-api-examples/index.php?source_dir=Roid-Library-master/src/com/rincliu/library/common/persistence/zxing/camera/CameraManager.java // This is the standard Android format which all devices are REQUIRED // to support. In theory, it's the only one we should ever care about. case ImageFormat.NV16: // Source: http://www.programcreek.com/java-api-examples/index.php?source_dir=Roid-Library-master/src/com/rincliu/library/common/persistence/zxing/camera/CameraManager.java // This format has never been seen in the wild, but is compatible as we only care // about the Y channel, so allow it. case ImageFormat.YV12: // source: https://github.com/evopark/tiqr-android/blob/master/src/main/java/de/evopark/tiqr/android/processing/ZxingQrScanner.java case ImageFormat.YUY2: // source: https://github.com/evopark/tiqr-android/blob/master/src/main/java/de/evopark/tiqr/android/processing/ZxingQrScanner.java case ImageFormat.YUV_420_888: // 420_888 is verified in practice - the other two case ImageFormat.YUV_422_888: // only varies from yuv_420_888 in chroma-subsampling, which I'm guessing // doesn't affect the luminance much // (see https://en.wikipedia.org/wiki/Chroma_subsampling) case ImageFormat.YUV_444_888: // only varies from yuv_420_888 in chroma-subsampling, which I'm guessing // doesn't affect the luminance much // (see https://en.wikipedia.org/wiki/Chroma_subsampling) return new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false); case ImageFormat.FLEX_RGB_888: case ImageFormat.FLEX_RGBA_8888: return new RGBLuminanceSource(width, height, uncompress(data, width, height));// PlanarYUVLuminanceSource(bytes, width, height, 0, 0, width, height, false); case ImageFormat.JPEG: // Tried and tested myself return new RGBLuminanceSource(width, height, uncompress(data, width, height));// PlanarYUVLuminanceSource(bytes, width, height, 0, 0, width, height, false); case ImageFormat.RGB_565: return new RGB565LuminanceSource(width, height, uncompress(data, width, height));// PlanarYUVLuminanceSource(bytes, width, height, 0, 0, width, height, false); case ImageFormat.RAW_SENSOR: case ImageFormat.RAW10: case ImageFormat.RAW12: case ImageFormat.DEPTH16: case ImageFormat.DEPTH_POINT_CLOUD: //ImageFormat.Y8: //ImageFormat.Y16: return null; default: return null; } }
public void zxing() throws ChecksumException, FormatException{ Bitmap bMap2 = Bitmap.createBitmap(qr_code2.width(),qr_code2.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(qr_code2, bMap2); int[] intArray2 = new int[bMap2.getWidth()*bMap2.getHeight()]; bMap2.getPixels(intArray2, 0, bMap2.getWidth(), 0, 0, bMap2.getWidth(), bMap2.getHeight()); LuminanceSource source2 = new RGBLuminanceSource(bMap2.getWidth(), bMap2.getHeight(),intArray2); BinaryBitmap bitmap2 = new BinaryBitmap(new HybridBinarizer(source2)); Reader reader2 = new QRCodeReader(); try { final Result result2 = reader2.decode(bitmap2); Log.d(TAG,"Found something 2: "+result2.getText()); runOnUiThread(new Runnable() { public void run() { m_QRcodeString2 = (""+result2.getText()); qr_message2.setText(m_QRcodeString2); ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("QR code content", qr_message2.getText()); clipboard.setPrimaryClip(clip); final Toast message = Toast.makeText(getApplicationContext(), "QR code content was copied to Clipboard", Toast. LENGTH_SHORT); message.show(); Handler handler2 = new Handler(); handler2.postDelayed(new Runnable() { @Override public void run() { message.cancel(); } }, 2000); } }); } catch (NotFoundException e) { Log.d(TAG, "Code Not Found!"); e.printStackTrace(); } }
private static BinaryBitmap imageToBitmap(BufferedImage image) { return new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(image.getWidth(), image.getHeight(), imageToRGB(image)))); }