在网上看到android中使用ZXing解二维码的网站,看到这部分代码不太懂:
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource( data, width, height, dstLeft, dstTop, dstWidth,dstHeight, false);
里面的参数不知道什么意思?
其实还是不知道那里面的参数是什么意思。问题其实想复杂了,使用ZXing给我们提供的方法就可以实现二维码的解码功能。
首先需要在项目中引用zxing库,右键项目,properties,点击android,点击下面的add按钮增加zxing库。
在AndroidMainfest.xml中加入:
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden" />
android:name="com.google.zxing.client.android.PreferencesActivity"
android:configChanges="orientation|keyboardHidden" />
在onActivityResult中使用CaptureActivity.getResult(MainActivity.this)获得解码结果。
android中dstLeft, dstTop, dstWidth,dstHeight
的用法是,根据当前图像的位图按比例缩小,生成指定高、宽等数据的新图像。这里有连接说明
我查看ZXing source code代码,没有boolean参数的构造函数
PlanarYUVLuminanceSource(byte[] yuvData, int dataWidth, int dataHeight, int left,
int top, int width, int height)
{
super(width, height);
if (left + width > dataWidth || top + height > dataHeight)
{
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}
this.yuvData = yuvData;
this.dataWidth = dataWidth;
this.dataHeight = dataHeight;
this.left = left;
this.top = top;
}