标签:
主要实现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> { @Override protected 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; } @Override protected 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 webview长按识别图片 ,利用zxing识别图片是否是二维码
标签:
原文地址:http://blog.csdn.net/xielinhua88/article/details/51356270