【Android 长按识别图中二维码 zxing】弱龄寄事外,委怀在琴书。这篇文章主要讲述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 = https://www.songbingjia.com/android/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]
推荐阅读
- ElasticsearchNEST高级客户端--Mapping映射
- Appium遇到的问题(持续更新....)
- Android控件之CalendarView 日历对话框
- Android开发中几种有用的的日历控件实现
- Java FileWriter类
- Java FilterInputStream类
- Java FileReader类
- Java FileDescriptor
- Java FilePermission类