Scanning QR codes using OpenCV and Zxing for Android

主宰稳场 提交于 2019-12-12 00:48:14

问题


I am using OpenCV (2.4.8) and Zxing (2.3.0) for Android and I'd like implement „hidden“ QR code scanning (without use Zxing CaptureActivity on screen) in Mat converted to bitmap and then display decoded result in console.

So, I call Zxing() methot in onCameraFrame method:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    // there will be preprocessing
    mRgba = inputFrame.rgba();

    try {
        zxing();
    } catch (ChecksumException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    return mRgba;
}

And it is my zxing() method (inspired by ZXing convert Bitmap to BinaryBitmap):

public void zxing() throws ChecksumException, FormatException{

    Bitmap bMap = Bitmap.createBitmap(mRgba.width(), mRgba.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mRgba, 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));
    Reader reader = new QRCodeMultiReader();     

    String sResult = "";

   try {

        Result result = reader.decode(bitmap);   
        sResult = result.getText();
        Log.d(TAG, sResult);

        }
    catch (NotFoundException e) {
            Log.d(TAG, "Code Not Found");
            e.printStackTrace();
    }

    }

With this code, I receive "Code Not Found" messages (about five per second) in LogCat console when camera don’t capture QR code, but when try scan QR code, I don’t see any message ( I thought I will receive sResult). What I have wrong?

Android manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    <activity
        android:name="mk.app_02_28.MainActivity"
        android:label="@string/app_name" 
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

回答1:


It was stupid mistake, I had wrong line

Log.d(TAG, sResult);

It should be:

Log.d(TAG,"Found something: "+result.getText());


来源:https://stackoverflow.com/questions/22145084/scanning-qr-codes-using-opencv-and-zxing-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!