拍照后获得的图片比较大,可以对图片进行放缩处理,减小图片的横纵像素点,可以提高二维码的解码速率。相关的代码如下:
private PictureCallback jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] _data, Camera _camera)
{
// TODO Handle JPEG image data
try
{
Bitmap bm = null;
//_data是拍照后图像的数据,这里将这些数据转换为bitmap对象
bm = BitmapFactory.decodeByteArray(_data, 0, _data.length);
//设置转换后的图片宽高分别为160,120.
int resizeWidth = 160;
int resizeHeight = 120;
float scaleWidth = ((float) resizeWidth) / bm.getWidth();
float scaleHeight = ((float) resizeHeight) / bm.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
//选取160*120图片正中的100*100像素再次创建一个Bitmap
Bitmap resizedBitmapSquare = Bitmap.createBitmap(resizedBitmap, 30, 10, 100, 100);
mImageView01.setImageBitmap(resizedBitmapSquare);
}
catch (Exception e)
{
Log.e(TAG, e.getMessage());
}
}
}
上面的程序是从拍照后的数据直接进行放缩,我们也可以从sd卡中读取图片转换成bitmap对象能后进行放缩操作。
String strQRTestFile = "/sdcard/test_qrcode.jpg";
File myImageFile = new File(strQRTestFile);
if(myImageFile.exists())
{
Bitmap myBmp = BitmapFactory.decodeFile(strQRTestFile);
}
后面的操作就和上面一样了。