发布于 2016-05-22 11:04:40 | 206 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现二维码的生成与解析方法,结合实例分析了Android二维码的生成与读取二维码的相关技巧,并提供了二维码jar包供读者下载,需要的朋友可以参考下

本文实例讲述了Android编程实现二维码的生成与解析。分享给大家供大家参考,具体如下:

直接上代码,代码上面有具体的解析,并且提供jar供下载:二维码Jar包.rar

根据文本生成对应的二维码:


// 生成QR图
private void createImage() {
  try {
   // 需要引入core包
   QRCodeWriter writer = new QRCodeWriter();
   String text = qr_text.getText().toString();
   Log.i(TAG, "生成的文本:" + text);
   if (text == null || "".equals(text) || text.length() < 1) {
    return;
   }
   // 把输入的文本转为二维码
   BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
     QR_WIDTH, QR_HEIGHT);
   System.out.println("w:" + martix.getWidth() + "h:"
     + martix.getHeight());
   Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
   hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
   BitMatrix bitMatrix = new QRCodeWriter().encode(text,
     BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
   int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
   for (int y = 0; y < QR_HEIGHT; y++) {
    for (int x = 0; x < QR_WIDTH; x++) {
     if (bitMatrix.get(x, y)) {
      pixels[y * QR_WIDTH + x] = 0xff000000;
     } else {
      pixels[y * QR_WIDTH + x] = 0xffffffff;
     }
    }
   }
   Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
     Bitmap.Config.ARGB_8888);
   bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
   qr_image.setImageBitmap(bitmap);
  } catch (WriterException e) {
   e.printStackTrace();
  }
}

根据二维码图片读取内容:


// 解析QR图片
private void scanningImage() {
  Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();
  hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
  // 获得待解析的图片
  Bitmap bitmap = ((BitmapDrawable) qr_image.getDrawable()).getBitmap();
  RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
  BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
  QRCodeReader reader = new QRCodeReader();
  Result result;
  try {
   result = reader.decode(bitmap1, hints);
   // 得到解析后的文字
   qr_result.setText(result.getText());
  } catch (NotFoundException e) {
   e.printStackTrace();
  } catch (ChecksumException e) {
   e.printStackTrace();
  } catch (FormatException e) {
   e.printStackTrace();
  }
}

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务