10

私は OpenCV と Zxing を使用しており、2 次元コードのスキャンを追加したいと考えています。送信できる画像の種類がいくつかあります。おそらく最適なのは Bitmat です (もう 1 つのオプションは OpenCV Mat です)。

以前は次のように変換できたようです。

Bitmap frame = //this is the frame coming in

LuminanceSource source = new RGBLuminanceSource(frame);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

//then I can use reader.decode(bitmap) to decode the BinaryBitmap

ただし、RGBLuminaceSource は、入力としてビットマップを使用しなくなったようです。では、他にどのように入力画像を BinaryBitmap に変換できますか?

編集:

わかりましたので、ある程度は進歩したと思いますが、まだ問題があります。ビットマップを正しい形式に変換するコードがあると思いますが、今は arrayIndexOutOfBounds を取得しています

public void zxing(){
    Bitmap bMap = Bitmap.createBitmap(frame.width(), frame.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(frame, bMap);
    byte[] array = BitmapToArray(bMap);
    LuminanceSource source = new PlanarYUVLuminanceSource(array, bMap.getWidth(), bMap.getHeight(), 0, 0, bMap.getWidth(), bMap.getHeight(), false);
        
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Reader reader = new DataMatrixReader();
    String sResult = "";
    try {
        Result result = reader.decode(bitmap);
        sResult = result.getText();
        Log.i("Result", sResult);
        }
    catch (NotFoundException e) {
            Log.d(TAG, "Code Not Found");
            e.printStackTrace();
    }
}

public byte[] BitmapToArray(Bitmap bmp){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

エラーが発生します

02-14 10:19:27.469: E/AndroidRuntime(29736): java.lang.ArrayIndexOutOfBoundsException: length=33341; index=34560 02-14 10:19:27.469: E/AndroidRuntime(29736):   at 
com.google.zxing.common.HybridBinarizer.calculateBlackPoints(HybridBinarizer.java:199)

byte[] のサイズをログに記録しました。これは上記の長さです。なぜzxingがもっと大きくなることを期待しているのか理解できません

4

3 に答える 3

14
public static String readQRImage(Bitmap bMap) {
    String contents = null;

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

    Reader reader = new MultiFormatReader();// use this otherwise ChecksumException
    try {
        Result result = reader.decode(bitmap);
        contents = result.getText(); 
        //byte[] rawBytes = result.getRawBytes(); 
        //BarcodeFormat format = result.getBarcodeFormat(); 
        //ResultPoint[] points = result.getResultPoints();
    } catch (NotFoundException e) { e.printStackTrace(); } 
    catch (ChecksumException e) { e.printStackTrace(); }
    catch (FormatException e) { e.printStackTrace(); } 
    return contents;
}
于 2014-03-17T00:31:36.413 に答える
2

BitmapAndroid クラスです。カメラからの Android のデフォルトの画像形式は、平面 YUV 形式です。PlanarYUVLuminanceSourceが必要であり、Android に存在するのはそのためです。RGBLuminanceSource移植する必要があります。

クラスに完全に間違った種類のデータを入れています。YUV 平面形式のピクセルが必要です。JPEG ファイルの圧縮バイトを渡しています。

于 2013-02-14T17:18:32.813 に答える