转载来自www.eoeandroid.com/forum.php?mod=viewthread&tid=332855&page=1&ordertype=1
提供NDK打包zbar源码帮助http://www.blackdogfoundry.com/blog/zbar-bar-code-qr-code-reader-android/
在此一并做一下记录。
修改Zbar有效扫描区域的关键代码,在CaptureActivity中initCamera函数中
CameraManager.get().openDriver(surfaceHolder);
Point point = CameraManager.get().getCameraResolution();
int width = point.y;
int height = point.x;
int x = mCropLayout.getLeft() * width / mContainer.getWidth();
int y = mCropLayout.getTop() * height / mContainer.getHeight();
int cropWidth = mCropLayout.getWidth() * width / mContainer.getWidth();
int cropHeight = mCropLayout.getHeight() * height / mContainer.getHeight() - (iHeight - height);// 由于扫描框的大小,比实际扫描的高度矮,故减去(iHeight - height)的高度
setX(x);
setY(y);
setCropWidth(cropWidth);
setCropHeight(cropHeight);
解码并保存图片的相关代码,在DecodeHandler中decode函数中
private void decode(byte[] data, int width, int height)
{
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;// Here we are swapping, that's the difference to #11
width = height;
height = tmp;
ZbarManager manager = new ZbarManager();
String result = manager.decode(rotatedData, width, height, true, activity.getX(), activity.getY(), activity.getCropWidth(), activity.getCropHeight());
if (result != null)
{
if (activity.isNeedCapture())
{
// 生成bitmap
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(rotatedData, width, height, activity.getX(), activity.getY(), activity.getCropWidth(),
activity.getCropHeight(), false);
int[] pixels = source.renderThumbnail();
int w = source.getThumbnailWidth();
int h = source.getThumbnailHeight();
Bitmap bitmap = Bitmap.createBitmap(pixels, 0, w, w, h, Bitmap.Config.ARGB_8888);
try
{
String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Qrcode/";
File root = new File(rootPath);
if (!root.exists())
{
root.mkdirs();
}
File f = new File(rootPath + "Qrcode.jpg");
if (f.exists())
{
f.delete();
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (null != activity.getHandler())
{
Message msg = new Message();
msg.obj = result;
msg.what = R.id.decode_succeeded;
activity.getHandler().sendMessage(msg);
}
}
else
{
if (null != activity.getHandler())
{
activity.getHandler().sendEmptyMessage(R.id.decode_failed);
}
}
}
界面自定义扫描框,可在CaptureActivity中自定义即可。也可仿照ZXing的ViewfinderView界面编写自己的布局。