【问题标题】:Scanning QR codes using OpenCV and Zxing for Android使用 OpenCV 和 Zxing for Android 扫描二维码
【发布时间】:2015-12-09 06:45:38
【问题描述】:

我在 Android 上使用 OpenCV (2.4.8) 和 Zxing (2.3.0),我想在 Mat 中实现“隐藏”二维码扫描(在屏幕上不使用 Zxing CaptureActivity)转换为位图然后在控制台中显示解码结果。

所以,我在onCameraFrame 方法中调用Zxing() 方法:

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

这是我的zxing() 方法(灵感来自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();
    }

    }

使用此代码,当相机未捕获 QR 码时,我在 LogCat 控制台中收到“找不到代码”消息(大约每秒 5 个),但是当尝试扫描 QR 码时,我没有看到任何消息(我以为我将收到 sResult)。 我有什么问题?

Android 清单:

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

【问题讨论】:

    标签: android opencv zxing


    【解决方案1】:

    这是一个愚蠢的错误,我错了行

    Log.d(TAG, sResult);
    

    应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多