Android 长按识别图中二维码 zxing
Posted 星辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]
以上是关于Android 长按识别图中二维码 zxing的主要内容,如果未能解决你的问题,请参考以下文章