OpenCV的使用和Zxing扫描QR codeS为Android [英] Scanning QR codes using OpenCV and Zxing for Android
问题描述
我使用的OpenCV(2.4.8)和Zxing(2.3.0)为Android,我想实现隐藏QR code扫描(不使用Zxing CaptureActivity $在
垫
C $ C>在屏幕上)转换为位图,然后显示德codeD导致控制台。
所以,我叫 Zxing()
methot在 onCameraFrame
方法:
公共垫onCameraFrame(CvCameraViewFrame inputFrame){ //会有preprocessing
mRgba = inputFrame.rgba(); 尝试{
zxing();
}赶上(ChecksumException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(FormatException E){
// TODO自动生成catch块
e.printStackTrace();
}
返回mRgba;
}
和这是我的 zxing()
法(的 ZXing转换位图到BinaryBitmap ):
公共无效zxing()抛出ChecksumException,FormatException { 位图BMAP = Bitmap.createBitmap(mRgba.width(),mRgba.height(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mRgba,BMAP);
INT [] intArray =新INT [bMap.getWidth()* bMap.getHeight()];
//从位图复制的像素数据进入intArray阵
bMap.getPixels(intArray,0,bMap.getWidth(),0,0,bMap.getWidth(),bMap.getHeight()); LuminanceSource源=新RGBLuminanceSource(bMap.getWidth(),bMap.getHeight(),intArray); BinaryBitmap位=新BinaryBitmap(新HybridBinarizer(源));
读卡器读卡器=新的QR codeMultiReader(); 串sResult =; 尝试{ 结果结果= reader.de code(位图);
sResult = result.getText();
Log.d(TAG,sResult); }
赶上(NotFoundException E){
Log.d(TAG,code未找到);
e.printStackTrace();
} }
有了这个code,我收到code未找到在LogCat中控制台消息(约五每秒)时,相机不拍摄QR code,但在尝试扫描QR code,我没有看到任何消息(我想我会收到sResult)。
我有什么错?
Android清单:
<应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen>
<活动
机器人:名字=mk.app_02_28.MainActivity
机器人:标签=@字符串/ APP_NAME
机器人:screenOrientation =风景>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
这是愚蠢的错误,我错了行
Log.d(TAG,sResult);
它应该是:
Log.d(TAG,找到东西:+ result.getText());
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>
It was stupid mistake, I had wrong line
Log.d(TAG, sResult);
It should be:
Log.d(TAG,"Found something: "+result.getText());
这篇关于OpenCV的使用和Zxing扫描QR codeS为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!