【发布时间】:2011-10-18 04:32:49
【问题描述】:
编辑:
玩了几个小时后,我开始相信问题出在图像质量上。例如,第一个图像是它来自相机的方式。解码器无法读取它。第二张图像经过调整后的对比度转换为黑白图像,解码器可以很好地读取它。
由于 zxing 附带的演示应用程序能够在几秒钟内从显示器读取第一张图像,我认为问题可能出在 zxing 库中的某些设置中。它没有等待足够长的时间来处理图像,但几乎立即吐出 NotFound。
我正在制作一个简单的二维码阅读器应用程序。这是一个屏幕截图。 顶部的黑色区域是表面视图,显示来自相机的帧。它工作正常,只是您在屏幕截图中看不到它。
然后,当我按下按钮时,从该表面视图中获取一个位图,放置在下面的 ImageView 上,并尝试由 zxing 库读取。
但它会发出 NotFoundException。 :/
**10-17 19:53:15.382: WARN/System.err(2238): com.google.zxing.NotFoundException
10-17 19:53:15.382: WARN/dalvikvm(2238): getStackTrace() called but no trace available**
另一方面,如果我从该屏幕截图中裁剪 qr 图像,将其放入 imageview(而不是相机源)并尝试对其进行解码,它可以正常工作。因此 QR 图像本身和它的质量都还可以......但是为什么它在第一种情况下不解码呢?
谢谢!
public void dec(View v)
{
ImageView ivCam2 = (ImageView)findViewById(R.id.imageView2);
ivCam2.setImageBitmap(bm);
BitmapDrawable drawable = (BitmapDrawable) ivCam2.getDrawable();
Bitmap bMap = drawable.getBitmap();
TextView textv = (TextView) findViewById(R.id.mytext);
LuminanceSource source = new RGBLuminanceSource(bMap);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap);
Global.text = result.getText();
byte[] rawBytes = result.getRawBytes();
BarcodeFormat format = result.getBarcodeFormat();
ResultPoint[] points = result.getResultPoints();
textv.setText(Global.text);
} catch (NotFoundException e) {
textv.setText("NotFoundException");
} catch (ChecksumException e) {
textv.setText("ChecksumException");
} catch (FormatException e) {
textv.setText("FormatException");
}
}
位图是如何创建的:
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
this.camera = Camera.open();
this.camera.setPreviewDisplay(this.holder);
this.camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] _data, Camera _camera) {
Camera.Parameters params = _camera.getParameters();
int w = params.getPreviewSize().width;
int h = params.getPreviewSize().height;
int format = params.getPreviewFormat();
YuvImage image = new YuvImage(_data, format, w, h, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Rect area = new Rect(0, 0, w, h);
image.compressToJpeg(area, 50, out);
bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
}
});
}
catch(IOException ioe)
{
ioe.printStackTrace(System.out);
}
}
【问题讨论】:
-
如何从 SurfaceView 获取位图?你在用
SurfaceView.getDrawingCache()吗? -
不,见上文。 :) 但我认为位图没问题,因为它显示在 imageview 上。
-
您能否将行
e.printStackTrace()添加到 catch 语句中,以便我们查看问题所在?