com.google.zxing.RGBLuminanceSource Java Examples

The following examples show how to use com.google.zxing.RGBLuminanceSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QRCodeDecode.java    From ZxingSupport with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example #2
Source File: DecodeUtils.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: ZXingDecoder.java    From react-native-barcode with MIT License 6 votes vote down vote up
@Override
public ReadableMap decodeRGBBitmap(Bitmap bitmap) {
    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);
    BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
    WritableMap result = null;
    try {
        Result decodeResult = mReader.decode(bBitmap, mHints);
        result = Arguments.createMap();
        result.putInt("format", symbolToFormat(decodeResult.getBarcodeFormat()));
        result.putString("content", decodeResult.getText());
    } catch (NotFoundException ignored) {
    }
    return result;
}
 
Example #4
Source File: DecodeUtils.java    From myapplication with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: ScannerActivity.java    From talk-android with MIT License 5 votes vote down vote up
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;
}
 
Example #6
Source File: QRReader.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * {@link Bitmap} から {@link LuminanceSource} を作成します.
 *
 * @param bitmap カメラのプレビュー画像
 * @return {@link LuminanceSource} のインスタンス
 */
private LuminanceSource createLuminanceSource(final Bitmap bitmap) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int[] intArray = new int[w * h];
    bitmap.getPixels(intArray, 0, w, 0, 0, w, h);
    return new RGBLuminanceSource(w, h, intArray);
}
 
Example #7
Source File: QrCodeHelper.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
/**
 * 直接识别二维码位图
 *
 * @param bitmap 二维码位图
 * @return 二维码包含的内容
 */
public static String recognizeQrCode(Bitmap bitmap)
{
    if (bitmap == null)
        return null;
    Hashtable<DecodeHintType, String> hints = new Hashtable<>();
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码
    //获取图片的像素存入到数组
    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    RGBLuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), pixels);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try
    {
        Result result = reader.decode(bitmap1, hints);
        if (reader == null)
        {
            return null;
        } else
        {
            String str = result.toString();
            boolean ISO = Charset.forName("ISO-8859-1").newEncoder().canEncode(str);
            if (ISO)
                str = new String(str.getBytes("ISO-8859-1"), "GB2312");
            return str;
        }
    } catch (Exception e)
    {
        Log.e("QrCodeHelper", "recognizeQrCode fail:" + e.toString());
    }
    return null;
}
 
Example #8
Source File: ImageProcessor.java    From Document-Scanner with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
 
Example #9
Source File: AbsCodec.java    From StarBarcode with Apache License 2.0 5 votes vote down vote up
/**
 * 使用RGB解析bitmap
 *
 * @param data
 * @param width
 * @param height
 * @param hintTypeMap
 * @return
 */
private Result decodeFromRGB(int[] data, int width, int height, Map<DecodeHintType, ?> hintTypeMap) {
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        return new MultiFormatReader().decode(binaryBitmap, hintTypeMap);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #10
Source File: MainActivity.java    From Aegis with GNU General Public License v3.0 5 votes vote down vote up
private void onScanImageResult(Intent intent) {
    Uri inputFile = (intent.getData());
    Bitmap bitmap;

    try {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();

        try (InputStream inputStream = getContentResolver().openInputStream(inputFile)) {
            bitmap = BitmapFactory.decodeStream(inputStream, null, bmOptions);
        }

        int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
        bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

        LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

        Reader reader = new MultiFormatReader();
        Result result = reader.decode(binaryBitmap);

        GoogleAuthInfo info = GoogleAuthInfo.parseUri(result.getText());
        VaultEntry entry = new VaultEntry(info);

        startEditEntryActivity(CODE_ADD_ENTRY, entry, true);
    } catch (NotFoundException | IOException | ChecksumException | FormatException | GoogleAuthInfoException e) {
        e.printStackTrace();
        Dialogs.showErrorDialog(this, R.string.unable_to_read_qrcode, e);
    }
}
 
Example #11
Source File: BarcodeUtils.java    From code-scanner with MIT License 5 votes vote down vote up
/**
 * Decode barcode from RGB pixels array
 *
 * @param pixels Colors in standard Android ARGB format
 * @param width  Image width
 * @param height Image height
 * @param hints  Decoder hints
 * @return Decode result, if barcode was decoded successfully, {@code null} otherwise
 * @see DecodeHintType
 * @see Color
 */
@Nullable
public static Result decodeRgb(@NonNull final int[] pixels, final int width, final int height,
        @Nullable final Map<DecodeHintType, ?> hints) {
    Objects.requireNonNull(pixels);
    final MultiFormatReader reader = createReader(hints);
    try {
        return Utils
                .decodeLuminanceSource(reader, new RGBLuminanceSource(width, height, pixels));
    } catch (final ReaderException e) {
        return null;
    }
}
 
Example #12
Source File: ScannerActivity.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_SELECT_PICTURE) {
        final Uri selectedUri = data.getData();
        if (selectedUri != null) {
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedUri);
                if (bitmap != null) {
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    int[] dataArr = new int[width * height];
                    bitmap.getPixels(dataArr, 0, width, 0, 0, width, height);
                    Decoder decoder = new Decoder(new MultiFormatReader());
                    Result result = decoder.decode(new RGBLuminanceSource(width, height, dataArr));
                    if (result != null) {
                        BrowserActivity.start(this, result.getText());
                    } else {
                        SnackbarUtil.show(this, "这个码真扫不了");
                    }
                    bitmap.recycle();
                } else {
                    SnackbarUtil.show(this, "这个码真扫不了");
                }
            } catch (Exception e) {
                SnackbarUtil.show(this, "这个码真扫不了");
            }
        } else {
            SnackbarUtil.show(this, "这个码真扫不了");
        }
    }
}
 
Example #13
Source File: QrCodeHelper.java    From adamant-android with GNU General Public License v3.0 5 votes vote down vote up
public String parse(InputStream imageStream) {
    final Bitmap bMap = BitmapFactory.decodeStream(imageStream);
    String contents = null;

    int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];
    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));

    Map<DecodeHintType, Object> tmpHintsMap = new EnumMap<DecodeHintType, Object>(
            DecodeHintType.class);
    tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS,
            EnumSet.allOf(BarcodeFormat.class));
    tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);

    Reader reader = new MultiFormatReader();
    Result result = null;
    try {
        result = reader.decode(bitmap, tmpHintsMap);
        contents = result.getText();
    } catch (NotFoundException | ChecksumException | FormatException e) {
        e.printStackTrace();
    }

    return contents;
}
 
Example #14
Source File: ImageProcessor.java    From react-native-documentscanner-android with MIT License 5 votes vote down vote up
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 e) {
        }

        return results;

    }
 
Example #15
Source File: ZXingQRCodeUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 解析二维码图片
 * @param bitmap         待解析的二维码图片
 * @param qrScanCallBack 解析结果回调
 */
public static void decodeQRCode(final Bitmap bitmap, final QRScanCallBack qrScanCallBack) {
    if (bitmap == null) {
        if (qrScanCallBack != null) {
            qrScanCallBack.onResult(false, null, new Exception("bitmap is null"));
        }
        return;
    }
    // 开始解析
    DevThreadManager.getInstance(5).execute(new Runnable() {
        @Override
        public void run() {
            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)), DECODE_HINTS);
                // 触发回调
                if (qrScanCallBack != null) {
                    qrScanCallBack.onResult((result != null), result, null);
                }
            } catch (Exception e) {
                LogPrintUtils.eTag(TAG, e, "decodeQRCode");
                // 触发回调
                if (qrScanCallBack != null) {
                    qrScanCallBack.onResult(false, null, e);
                }
            }
        }
    });
}
 
Example #16
Source File: RxQrCode.java    From RxQrCode with MIT License 4 votes vote down vote up
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);
    });
}
 
Example #17
Source File: RCTQRCodeLocalImage.java    From react-native-qrcode-local-image with MIT License 4 votes vote down vote up
@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");
    }
}
 
Example #18
Source File: QRCodeScanActivity.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@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();

      int[] newPixels = null;
      for (int i = 0; i < 4; i++) {
        if (i > 0) {
          newPixels = BitmapUtils.rotate(pixels, width, height, newPixels);
          int temp = width;
          width = height;
          height = temp;
          int[] tempPixels = pixels;
          pixels = newPixels;
          newPixels = tempPixels;
        }

        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<>();

        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) {
            // pass
          }
        }
      }

      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();
}