A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老衲玩IT 中级黑马   /  2013-8-27 14:15  /  1369 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class MainActivity extends Activity{
  2.          public  void onCreate(Bundle bundle) {
  3.                  super.onCreate(bundle);
  4.                 //加密二维码
  5.                 encodeQRcode("sdcard/hwy.jpg","http://bbs.itheima.com/forum-93-1.html");
  6.                 //解码二维码
  7.                 Log.v("Decode",decodeQRcode("sdcard/hwy.jpg"));
  8. }
  9.         
  10.         /**
  11.          *  把相关信息加密成二维码图片
  12.          * @param saveImgPath 图片的保存位置
  13.          * @param content 要生成的二维码内容
  14.          */
  15.         public static void encodeQRcode(String saveImgPath,String content) {
  16.                 encodeQRCode(saveImgPath,content,150,150);
  17.         }
  18.         /**
  19.          * 把相关信息加密成图片,并指定生成图片的大小
  20.          * @param saveImgPath 二给码图片保存位置
  21.          * @param content        二给码的内容
  22.          */
  23.         public static void encodeQRCode(String saveImgPath,String content,int width,int height) {
  24.                 try {
  25.                         BitMatrix byteMatrix = new MultiFormatWriter().encode(
  26.                                         new String(content.getBytes("GBK"),"iso-8859-1"),BarcodeFormat.QR_CODE, width, height);
  27.                         MatrixToImageWriter.writeToFile(byteMatrix, "jpg", new File(saveImgPath));        
  28.                 } catch (Exception e) {        
  29.                         e.printStackTrace();
  30.                 }
  31.         }

  32.         /**
  33.          * 把指定的二维码图片解码为字符串信息
  34.          */
  35.         public static String decodeQRcode(String imgPath) {
  36.                 try {
  37.                         File file = new File(imgPath);
  38.                         try {
  39.                                 BufferedImage image = ImageIO.read(file);
  40.                                 if (image == null) {
  41.                                         System.out.println("找不到解码图片");
  42.                                 }
  43.                                 LuminanceSource source = new BufferedImageLuminanceSource(image);
  44.                                 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  45.                                 Result result;
  46.                                 Hashtable<DecodeHintType,String> hints = new Hashtable<DecodeHintType,String>();
  47.                                 hints.put(DecodeHintType.CHARACTER_SET, "GBK");
  48.                                 result = new MultiFormatReader().decode(bitmap, hints);
  49.                                 String resultStr = result.getText();
  50.                                 return resultStr;
  51.                         } catch (IOException ioe) {
  52.                                 ioe.printStackTrace();
  53.                         } catch (ReaderException re) {
  54.                                 re.printStackTrace();
  55.                         }
  56.                 } catch (Exception ex) {
  57.                         ex.printStackTrace();
  58.                 }
  59.                 return null;
  60.         }
  61. }
复制代码

评分

参与人数 1黑马币 +3 收起 理由
EYE_SEE_YOU + 3 不要老发分享帖,应该多发技术贴.

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
请问楼主, BufferedImage image在android中怎么引用的?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马