码迷,mamicode.com
首页 > 移动开发 > 详细

Android 长按识别图中二维码 zxing

时间:2017-09-11 19:43:31      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:etc   选择   undefined   tpi   override   code   width   save   listen   

技术分享

#基于 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

标签:etc   选择   undefined   tpi   override   code   width   save   listen   

原文地址:http://www.cnblogs.com/zhujiabin/p/7506214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!