Java Code Examples for com.google.zxing.Reader#decode()

The following examples show how to use com.google.zxing.Reader#decode() . 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: BarcodeScanCameraActivity.java    From android-opensource-library-56 with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 2
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 3
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);
    }
}