android webview长按识别图片 ,利用zxing识别图片是否是二维码
2016-05-09 21:13
603 查看
主要实现webwiew的OnLongClick()方法 ,利用
final HitTestResult htr = getHitTestResult();//获取所点击的内容
判断点击的是不是图片 ,其主要代码是:
@Override public boolean onLongClick(View v) { // 长按事件监听(注意:需要实现LongClickCallBack接口并传入对象) final HitTestResult htr = getHitTestResult();//获取所点击的内容 if (htr.getType() == HitTestResult.IMAGE_TYPE || htr.getType() == HitTestResult.IMAGE_ANCHOR_TYPE || htr.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { //判断被点击的类型为图片 if (mCallBack!=null) { mCallBack.onLongClickCallBack(htr.getExtra()); } } return false; }如果判断是为图片后就可以利用zxing 来识别是否是二维码图片了:
@Override public void onLongClickCallBack(final String imgUrl) { imgurl=imgUrl; // 获取到图片地址后做相应的处理 new Thread(){ public void run() { decodeImage(imgUrl); handler.sendEmptyMessage(0); }; }.start(); showDialog(); }
首先 我们把图片的链接地址 转换成图片:
/*** 根据地址获取网络图片* @param sUrl 图片地址* @return* @throws IOException*/public static Bitmap getBitmap(String sUrl){try {URL url = new URL(sUrl);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");if(conn.getResponseCode() == 200){InputStream inputStream = conn.getInputStream();Bitmap bitmap = BitmapFactory.decodeStream(inputStream);return bitmap;}} catch (Exception e) {e.printStackTrace();}return null;}把链接转出图片了后在识别图片是不是二维码图:
/*** 判断是否为二维码* @param sUrl 图片地址* @return*/private boolean decodeImage(String sUrl){result = DecodeImage.handleQRCodeFormBitmap(getBitmap(sUrl));if(result == null){isQR = false;}else {isQR = true;}return isQR;}如果是二维码图片的画 发送handle 来更新adapter来显示“识别图中二维码”
@SuppressLint("HandlerLeak")private Handler handler = new Handler(){public void handleMessage(Message msg) {if (msg.what == 0){if (isQR){adapter.add("识别图中二维码");}adapter.notifyDataSetChanged();}};};点击保存图片了的话 使用asnytask线程来保存图片到本地 :
/**** 功能:用线程保存图片** @author wangyp**/private class SaveImage extends AsyncTask<String, Void, String> {@Overrideprotected String doInBackground(String... params) {String result = "";try {String sdcard;if (FileUtils.SDisExists()){sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();}else{sdcard=getFilesDir().getAbsolutePath();}File file = new File(sdcard + "/Download");if (!file.exists()) {file.mkdirs();}file = new File(sdcard + "/Download/" + new Date().getTime() + ".jpg");InputStream inputStream = null;URL url = new URL(imgurl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(20000);if (conn.getResponseCode() == 200) {inputStream = conn.getInputStream();}byte[] buffer = new byte[4096];int len = 0;FileOutputStream outStream = new FileOutputStream(file);while ((len = inputStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();result = "图片已保存至:" + file.getAbsolutePath();} catch (Exception e) {result = "保存失败!" + e.getLocalizedMessage();}return result;}@Overrideprotected void onPostExecute(String result) {Toast.makeText(H5Activity.this,result,Toast.LENGTH_SHORT).show();}}如果用户点击 识别二维码 的话 把识别好了的二维码链接打开浏览器:
Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse(result.toString()));startActivity(intent);closeDialog();这是demo点击打开链接下载
相关文章推荐
- 在低版本android系统上实现Material design应用
- Android新增API之AudioEffect中文API与应用实例
- Activity之affinity
- Android 6.0 向用户申请权限,运行时权限
- XMPP在iOS上使用-翻译文档
- poj2773 Happy 2006(二分+容斥)
- Android Dev Intro - An OpenGL ES renderer based on the GLSurfaceView rendering framework
- 大话Android项目中的常用技巧
- Android 设定横屏,禁止屏幕旋转,Activity重置
- Android中跨进程通信方式之使用文件共享
- 15 个 Android 通用流行框架大全
- AppWidget手把手教你(一步步很明白)
- iOS学习——NSString属性的strong和copy
- iOS获取UDID问题
- Android学习笔记之Activity的4种启动模式
- [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
- 获取当前Activity及Fragment名字
- 玩转Android之数据库框架ActiveAndroid的使用
- 玩转Android之数据库框架ActiveAndroid的使用
- 玩转Android之数据库框架ActiveAndroid的使用