Android 长按识别图中二维码 zxing

时间:2022-11-17 00:26:33

Android 长按识别图中二维码 zxing

#基于 Zxing, 初学Android  代码质量不高

//长按,通过zxing读取图片,判断是否有二维码
bigImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View viewm) {
Bitmap obmp
= ((BitmapDrawable) (bigImage).getDrawable()).getBitmap();
int width = obmp.getWidth();
int height = obmp.getHeight();
int[] data = new int[width * height];
obmp.getPixels(data,
0, width, 0, 0, width, height);
RGBLuminanceSource source
= new RGBLuminanceSource(width, height, data);
BinaryBitmap bitmap1
= new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader
= new QRCodeReader();
Result re
= null;
try {
re
= reader.decode(bitmap1);
}
catch (NotFoundException e) {
e.printStackTrace();
}
catch (ChecksumException e) {
e.printStackTrace();
}
catch (FormatException e) {
e.printStackTrace();
}
if (re == null) {
showAlert(obmp);
}
else {
showSelectAlert(obmp, re.getText());
}
return false;
}
});


private void showAlert(final Bitmap bitmap) {
AlertDialog.Builder builder
= new AlertDialog.Builder(this);
builder.setMessage(
"保存图片")
.setCancelable(
false)
.setPositiveButton(
"确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterfacem, int i) {
saveImageToGallery(bitmap);
}
})
.setNegativeButton(
"取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterfacem, int i) {
}
});
builder.show();
}

private void showSelectAlert(final Bitmap bitmap, final String url) {
AlertDialog.Builder builder
= new AlertDialog.Builder(this);
builder.setTitle(
"请选择");
String str[]
= {"保存图片", "扫二维码"};
builder.setItems(str,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterfacem, int i) {
switch (i) {
case 0: {
saveImageToGallery(bitmap);
}
break;
case 1: {
Intent n
= new Intent(EnlargeimagevActivity.this, DetailActivity.class);
n.putExtra(DetailActivity.BUNDLE_KEY_DISPLAY_TYPE, DetailActivity.WEBVIEW_DETAIL);
n.putExtra(DetailwebFragment.WEB_URL, url);
startActivity(n);
}
break;
}
}
});
builder.setNegativeButton(
"取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterfacem, int i) {

}
});
builder.show();
}
zxing 下载  [http://download.csdn.net/detail/eric19920720/9709305]