当前位置: 移动技术网 > 移动技术>移动开发>Android > Android从图片获取二维码的方法

Android从图片获取二维码的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

之前的博客我记得讲过关于扫描二维码的内容,昨天,组长让我不仅可以扫描获取二维码,还可以通过图片获取里面的二维码。比如别人拍了一张二维码的照片,发送给你,app应该可以获取图片的二维码。

自己在网上查了资料,发现其实也很简单,用zxing jar包里的获取图片二维码的qrcodereader就基本可以了。不过大部分的内容,我自己也不明白,大家如果有兴趣,可以自己去查找资料。

1.点击按钮后,跳转到相册,选择有二维码的图片,返回到解析二维码的界面。这时通过返回的uri获取图片的路径。

 case choose_pic:
          string[] proj = new string[]{mediastore.images.media.data};
          cursor cursor = qrcodeactivity.this.getcontentresolver().query(data.getdata(), proj, null, null, null);

          if(cursor.movetofirst()){
            int columnindex = cursor.getcolumnindex(mediastore.images.media.data);
            system.out.println(columnindex);
            //获取到用户选择的二维码图片的绝对路径
            imgpath = cursor.getstring(columnindex);
          }
          cursor.close();

          //获取解析结果
          result ret = parseqrcodebitmap(imgpath);
          if (ret==null){
            toast.maketext(qrcodeactivity.this,getstring(r.string.load_two_dimensional_error), toast.length_long).show();
          }else {
//            toast.maketext(qrcodeactivity.this,"解析结果:" + ret.tostring(), toast.length_long).show();
            intent intent = new intent();
            intent.putextra(intents.scan.result, ret.tostring());
            this.setresult(activity.result_ok, intent);
            this.overridependingtransition(android.r.anim.fade_in, android.r.anim.fade_out);
            finish();
          }
          break;

这个就是通过contentresolver查询uri获取图片的路径,然后调用parseqrcodebitmap(imgpath)获取图片的二维码。

2.通过图片路径进行解析图片,获取图片的二维码值。

//解析二维码图片,返回结果封装在result对象中
private com.google.zxing.result parseqrcodebitmap(string bitmappath){
  //解析转换类型utf-8
  hashtable<decodehinttype, string> hints = new hashtable<decodehinttype, string>();
  hints.put(decodehinttype.character_set, "utf-8");
  //获取到待解析的图片
  bitmapfactory.options options = new bitmapfactory.options();
  //如果我们把injustdecodebounds设为true,那么bitmapfactory.decodefile(string path, options opt)
  //并不会真的返回一个bitmap给你,它仅仅会把它的宽,高取回来给你
  options.injustdecodebounds = true;
  //此时的bitmap是null,这段代码之后,options.outwidth 和 options.outheight就是我们想要的宽和高了
  bitmap bitmap = bitmapfactory.decodefile(bitmappath,options);
  //我们现在想取出来的图片的边长(二维码图片是正方形的)设置为400像素
  /**
   options.outheight = 400;
   options.outwidth = 400;
   options.injustdecodebounds = false;
   bitmap = bitmapfactory.decodefile(bitmappath, options);
   */
  //以上这种做法,虽然把bitmap限定到了我们要的大小,但是并没有节约内存,如果要节约内存,我们还需要使用insimplesize这个属性
  options.insamplesize = options.outheight / 400;
  if(options.insamplesize <= 0){
    options.insamplesize = 1; //防止其值小于或等于0
  }
  /**
   * 辅助节约内存设置
   *
   * options.inpreferredconfig = bitmap.config.argb_4444;  // 默认是bitmap.config.argb_8888
   * options.inpurgeable = true;
   * options.ininputshareable = true;
   */
  options.injustdecodebounds = false;
  bitmap = bitmapfactory.decodefile(bitmappath, options);
  //新建一个rgbluminancesource对象,将bitmap图片传给此对象
  rgbluminancesource rgbluminancesource = new rgbluminancesource(bitmap);
  //将图片转换成二进制图片
  binarybitmap binarybitmap = new binarybitmap(new hybridbinarizer(rgbluminancesource));
  //初始化解析对象
  qrcodereader reader = new qrcodereader();
  //开始解析
  result result = null;
  try {
    result = reader.decode(binarybitmap, hints);
  } catch (exception e) {
    // todo: handle exception
  }

  return result;
}

这里首先获取图片的bitmap,需要把获取的bitmap专为一定的大小,通过options.insamplesize来实现,然后通过

//新建一个rgbluminancesource对象,将bitmap图片传给此对象
rgbluminancesource rgbluminancesource = new rgbluminancesource(bitmap);
//将图片转换成二进制图片
binarybitmap binarybitmap = new binarybitmap(new hybridbinarizer(rgbluminancesource));
//初始化解析对象
qrcodereader reader = new qrcodereader();

将bitmap的二维码转换成图片,然后又将图片转成二进制图片,调用qrcodereader的result = reader.decode(binarybitmap, hints);代码把二进制图片转成二维码,然后直接获取返回值的字符串就是二维码值。

其中用到了一个自定义的类rgbluminancesource,主要功能是将图片的二维码内容获取到,把除二维码的内容过滤,方便接下来的解析二维码。

package com.zwcode.p6spro.util;
import java.io.filenotfoundexception;

import android.graphics.bitmap;
import android.graphics.bitmapfactory;

import com.google.zxing.luminancesource;

public class rgbluminancesource extends luminancesource {
  private final byte[] luminances;

  public rgbluminancesource(bitmap bitmap) {
    super(bitmap.getwidth(), bitmap.getheight());
    //得到图片的宽高
    int width = bitmap.getwidth();
    int height = bitmap.getheight();
    //得到图片的像素
    int[] pixels = new int[width * height];
    //
    bitmap.getpixels(pixels, 0, width, 0, 0, width, height);
    //为了测量纯解码速度,我们将整个图像灰度阵列前面,这是一样的通道
    // yuvluminancesource在现实应用。
    //得到像素大小的字节数
    luminances = new byte[width * height];
    //得到图片每点像素颜色
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        int pixel = pixels[offset + x];
        int r = (pixel >> 16) & 0xff;
        int g = (pixel >> 8) & 0xff;
        int b = pixel & 0xff;
        //当某一点三种颜色值相同时,相应字节对应空间赋值为其值
        if (r == g && g == b) {
          luminances[offset + x] = (byte) r;
        }
        //其它情况字节空间对应赋值为:
        else {
          luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
        }
      }
    }
  }

  public rgbluminancesource(string path) throws filenotfoundexception {
    this(loadbitmap(path));
  }


  @override
  public byte[] getmatrix() {
    return luminances;
  }

  @override
  public byte[] getrow(int arg0, byte[] arg1) {
    if (arg0 < 0 || arg0 >= getheight()) {
      throw new illegalargumentexception(
          "requested row is outside the image: " + arg0);
    }
    int width = getwidth();
    if (arg1 == null || arg1.length < width) {
      arg1 = new byte[width];
    }
    system.arraycopy(luminances, arg0 * width, arg1, 0, width);
    return arg1;
  }

  private static bitmap loadbitmap(string path) throws filenotfoundexception {
    bitmap bitmap = bitmapfactory.decodefile(path);
    if (bitmap == null) {
      throw new filenotfoundexception("couldn't open " + path);
    }
    return bitmap;
  }
}

这样就可以识别图片的二维码了,用这个功能一定要先导入zxing jar包,这个很简单,网上有很多介绍,大家自己可以查找一下。

android 从图片获取二维码就讲完了。

就这么简单。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网