欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android二维码创建实例

程序员文章站 2022-10-24 17:06:49
android二维码之创建 实现效果图: 1.android 有自带的jar包可以生成二维码core-3.0.0.jar,其中的com.google.zxing包...

android二维码之创建

实现效果图:

Android二维码创建实例

1.android 有自带的jar包可以生成二维码core-3.0.0.jar,其中的com.google.zxing包

2.写一个二维码生成的工具类,网上搜的话应该一大堆。

实例代码:

package com.example.administrator.twocodedemo;

import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.pointf;
import android.view.gravity;
import android.view.view.measurespec;
import android.widget.linearlayout;
import android.widget.linearlayout.layoutparams;
import android.widget.textview;

import com.google.zxing.barcodeformat;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatwriter;
import com.google.zxing.writerexception;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.qrcodewriter;

import java.util.hashtable;

/** 
*
*   生成条形码和二维码的工具
*/
public class zxingutils {
  /**
   * 生成二维码 要转换的地址或字符串,可以是中文
   * 
   * @param url
   * @param width
   * @param height
   * @return
   */
  public static bitmap createqrimage(string url, final int width, final int height) {
    try {
      // 判断url合法性
      if (url == null || "".equals(url) || url.length() < 1) {
        return null;
      }
      hashtable<encodehinttype, string> hints = new hashtable<encodehinttype, string>();
      hints.put(encodehinttype.character_set, "utf-8");
      // 图像数据转换,使用了矩阵转换
      bitmatrix bitmatrix = new qrcodewriter().encode(url,
          barcodeformat.qr_code, width, height, hints);
      int[] pixels = new int[width * height];
      // 下面这里按照二维码的算法,逐个生成二维码的图片,
      // 两个for循环是图片横列扫描的结果
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          if (bitmatrix.get(x, y)) {
            pixels[y * width + x] = 0xff000000;
          } else {
            pixels[y * width + x] = 0xffffffff;
          }
        }
      }
      // 生成二维码图片的格式,使用argb_8888
      bitmap bitmap = bitmap.createbitmap(width, height,
          bitmap.config.argb_8888);
      bitmap.setpixels(pixels, 0, width, 0, 0, width, height);
      return bitmap;
    } catch (writerexception e) {
      e.printstacktrace();
    }
    return null;
  }

  /**
   * 生成条形码
   *
   * @param context
   * @param contents
   *      需要生成的内容
   * @param desiredwidth
   *      生成条形码的宽带
   * @param desiredheight
   *      生成条形码的高度
   * @param displaycode
   *      是否在条形码下方显示内容
   * @return
   */
  public static bitmap creatbarcode(context context, string contents,
                   int desiredwidth, int desiredheight, boolean displaycode) {
    bitmap ruseltbitmap = null;
    /**
     * 图片两端所保留的空白的宽度
     */
    int marginw = 20;
    /**
     * 条形码的编码类型
     */
    barcodeformat barcodeformat = barcodeformat.code_128;

    if (displaycode) {
      bitmap barcodebitmap = encodeasbitmap(contents, barcodeformat,
          desiredwidth, desiredheight);
      bitmap codebitmap = creatcodebitmap(contents, desiredwidth + 2
          * marginw, desiredheight, context);
      ruseltbitmap = mixturebitmap(barcodebitmap, codebitmap, new pointf(
          0, desiredheight));
    } else {
      ruseltbitmap = encodeasbitmap(contents, barcodeformat,
          desiredwidth, desiredheight);
    }

    return ruseltbitmap;
  }

  /**
   * 生成条形码的bitmap
   *
   * @param contents
   *      需要生成的内容
   * @param format
   *      编码格式
   * @param desiredwidth
   * @param desiredheight
   * @return
   * @throws writerexception
   */
  protected static bitmap encodeasbitmap(string contents,
                      barcodeformat format, int desiredwidth, int desiredheight) {
    final int white = 0xffffffff;
    final int black = 0xff000000;

    multiformatwriter writer = new multiformatwriter();
    bitmatrix result = null;
    try {
      result = writer.encode(contents, format, desiredwidth,
          desiredheight, null);
    } catch (writerexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }

    int width = result.getwidth();
    int height = result.getheight();
    int[] pixels = new int[width * height];
    // all are 0, or black, by default
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        pixels[offset + x] = result.get(x, y) ? black : white;
      }
    }

    bitmap bitmap = bitmap.createbitmap(width, height,
        bitmap.config.argb_8888);
    bitmap.setpixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }

  /**
   * 生成显示编码的bitmap
   *
   * @param contents
   * @param width
   * @param height
   * @param context
   * @return
   */
  protected static bitmap creatcodebitmap(string contents, int width,
                      int height, context context) {
    textview tv = new textview(context);
    linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(
        layoutparams.match_parent, layoutparams.wrap_content);
    tv.setlayoutparams(layoutparams);
    tv.settext(contents);
    tv.setheight(height);
    tv.setgravity(gravity.center_horizontal);
    tv.setwidth(width);
    tv.setdrawingcacheenabled(true);
    tv.settextcolor(color.black);
    tv.measure(measurespec.makemeasurespec(0, measurespec.unspecified),
        measurespec.makemeasurespec(0, measurespec.unspecified));
    tv.layout(0, 0, tv.getmeasuredwidth(), tv.getmeasuredheight());

    tv.builddrawingcache();
    bitmap bitmapcode = tv.getdrawingcache();
    return bitmapcode;
  }

  /**
   * 将两个bitmap合并成一个
   * 
   * @param first
   * @param second
   * @param frompoint
   *      第二个bitmap开始绘制的起始位置(相对于第一个bitmap)
   * @return
   */
  protected static bitmap mixturebitmap(bitmap first, bitmap second,
                     pointf frompoint) {
    if (first == null || second == null || frompoint == null) {
      return null;
    }
    int marginw = 20;
    bitmap newbitmap = bitmap.createbitmap(
        first.getwidth() + second.getwidth() + marginw,
        first.getheight() + second.getheight(), config.argb_4444);
    canvas cv = new canvas(newbitmap);
    cv.drawbitmap(first, marginw, 0, null);
    cv.drawbitmap(second, frompoint.x, frompoint.y, null);
    cv.save(canvas.all_save_flag);
    cv.restore();

    return newbitmap;
  }

}
zxingutils

3.mainactivity

@onclick({r.id.btn_create, r.id.iv_two_code}) 
  public void onclick(view view) { 
    switch (view.getid()) { 
      case r.id.btn_create: 
  
        string url = eturl.gettext().tostring().trim(); 
        bitmap bitmap = zxingutils.createqrimage(url, ivtwocode.getwidth(), ivtwocode.getheight()); 
        ivtwocode.setimagebitmap(bitmap); 

例如:

string company=etcompany.gettext().tostring().trim() ;
        string phone =etphone .gettext().tostring().trim() ;
        string email = etemail.gettext().tostring().trim() ;
        string web = etweb.gettext().tostring().trim() ;
        //二维码中包含的文本信息
        string contents= "begin:vcard\nversion:3.0\norg:"+company+"\ntel:"+phone+"\nurl:"+web+"\nemail:"+email+"\nend:vcard";
      try {
        //调用方法createcode生成二维码
    bitmap bm=createcode(contents, logo, barcodeformat.qr_code);
    

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!