Search in sources :

Example 76 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project wechat by motianhuo.

the class DecodeHandler method decode.

/**
 * 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.
 *
 * @param data
 *            The YUV preview frame.
 * @param width
 *            The width of the preview frame.
 * @param height
 *            The height of the preview frame.
 */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    // modify here
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    // Here we are swapping, that's the difference to #11
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
    // continue
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        // Log.d(TAG, "Sending decode succeeded message...");
        message.sendToTarget();
    } else {
        Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.juns.wechat.zxing.camera.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 77 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project BarcodeEye by BarcodeEye.

the class DecodeHandler method decode.

/**
 * 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.
 *
 * @param data   The YUV preview frame.
 * @param width  The width of the preview frame.
 * @param height The height of the preview frame.
 */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 78 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project zxingfragmentlib by mitoyarzun.

the class DecodeHandler method decode.

/**
 * 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.
 *
 * @param data   The YUV preview frame.
 * @param width  The width of the preview frame.
 * @param height The height of the preview frame.
 */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source = fragment.getCameraManager().buildLuminanceSource(data, width, height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = fragment.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, IConstants.DECODE_SUCCEDED, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, IConstants.DECODE_FAILED);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 79 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project JustAndroid by chinaltz.

the class CodeUtils method analyzeBitmap.

/**
 * 解析二维码图片工具类
 *
 * @param analyzeCallback
 */
public static void analyzeBitmap(String path, AnalyzeCallback analyzeCallback) {
    /**
     * 首先判断图片的大小,若图片过大,则执行图片的裁剪操作,防止OOM
     */
    BitmapFactory.Options options = new BitmapFactory.Options();
    // 先获取原大小
    options.inJustDecodeBounds = true;
    Bitmap mBitmap = BitmapFactory.decodeFile(path, options);
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 400);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    mBitmap = BitmapFactory.decodeFile(path, options);
    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(mBitmap))));
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (rawResult != null) {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeSuccess(mBitmap, rawResult.getText());
        }
    } else {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeFailed();
        }
    }
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) BarcodeFormat(com.google.zxing.BarcodeFormat) HybridBinarizer(com.google.zxing.common.HybridBinarizer) WriterException(com.google.zxing.WriterException) Result(com.google.zxing.Result) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BitmapLuminanceSource(com.litingzhe.justandroid.someOther.qrCode.zxingcamera.BitmapLuminanceSource) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) Vector(java.util.Vector)

Example 80 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project react-native-camera by lwansbrough.

the class BarCodeScannerAsyncTask method doInBackground.

@Override
protected Result doInBackground(Void... ignored) {
    if (isCancelled() || mDelegate == null) {
        return null;
    }
    /**
     * mCameraViewWidth and mCameraViewHeight are obtained from portait orientation
     * mWidth and mHeight are measured with landscape orientation with Home button to the right
     * adjustedCamViewWidth is the adjusted width from the Aspect ratio setting
     */
    int adjustedCamViewWidth = (int) (mCameraViewHeight / mRatio);
    float adjustedScanY = (((adjustedCamViewWidth - mCameraViewWidth) / 2) + (mScanAreaY * mCameraViewWidth)) / adjustedCamViewWidth;
    int left = (int) (mScanAreaX * mWidth);
    int top = (int) (adjustedScanY * mHeight);
    int scanWidth = (int) (mScanAreaWidth * mWidth);
    int scanHeight = (int) (((mScanAreaHeight * mCameraViewWidth) / adjustedCamViewWidth) * mHeight);
    try {
        try {
            BinaryBitmap bitmap = generateBitmapFromImageData(mImageData, mWidth, mHeight, false, left, top, scanWidth, scanHeight);
            return mMultiFormatReader.decodeWithState(bitmap);
        } catch (NotFoundException e) {
        }
        try {
            BinaryBitmap bitmap = generateBitmapFromImageData(rotateImage(mImageData, mWidth, mHeight), mHeight, mWidth, false, mHeight - scanHeight - top, left, scanHeight, scanWidth);
            return mMultiFormatReader.decodeWithState(bitmap);
        } catch (NotFoundException e) {
        }
        try {
            BinaryBitmap invertedBitmap = generateBitmapFromImageData(mImageData, mWidth, mHeight, true, mWidth - scanWidth - left, mHeight - scanHeight - top, scanWidth, scanHeight);
            return mMultiFormatReader.decodeWithState(invertedBitmap);
        } catch (NotFoundException e) {
        }
        try {
            BinaryBitmap invertedRotatedBitmap = generateBitmapFromImageData(rotateImage(mImageData, mWidth, mHeight), mHeight, mWidth, true, top, mWidth - scanWidth - left, scanHeight, scanWidth);
            return mMultiFormatReader.decodeWithState(invertedRotatedBitmap);
        } catch (NotFoundException e) {
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
    // no barcode found
    return null;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)81 Result (com.google.zxing.Result)64 HybridBinarizer (com.google.zxing.common.HybridBinarizer)63 ReaderException (com.google.zxing.ReaderException)40 Message (android.os.Message)25 Bundle (android.os.Bundle)24 MultiFormatReader (com.google.zxing.MultiFormatReader)21 NotFoundException (com.google.zxing.NotFoundException)21 BufferedImage (java.awt.image.BufferedImage)21 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)16 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 Hashtable (java.util.Hashtable)12 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 Bitmap (android.graphics.Bitmap)8 BitArray (com.google.zxing.common.BitArray)8