android 生成二维码

首先要有zxing包  http://download.csdn.net/detail/greehand_/9495295

布局就放一个ImagView 用于显示二维码
<ImageView 
       android:id="@+id/imgs"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

主代码
public class MainActivity extends Activity {
	private int QR_HEIGHT = 300;
	private int QR_WIDTH = 300;
	ImageView imgs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 1800/30=60*4=240
        imgs=(ImageView) findViewById(R.id.imgs);
        createImage("http://www.dianping.com/shop/5924161");//扫描二维码后所进去的页面
        
    }
    
    /**
	 * 产生二维码图片 方法名:createImage
	 * 
	 * @param text
	 */
	private void createImage(String text) {
		try {
			// 需要引入core包
			QRCodeWriter writer = new QRCodeWriter();
			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);

			// 自定义个性化二维码 中间放图标或者用户图像
			// 1-缩放用户图像大小50*50二维码中间的自定义图像区 不能太大 ,个人亲测超过50扫描会有障碍。
			Bitmap icon = BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_launcher);
			Bitmap icon2 = scaleBitmap(icon, 50, 50);// 二维码中间的自定义图像区 不能太大
														// ,个人亲测超过50扫描会有障碍。
			// 2-将用户图像画在二维码中间, 千万别覆盖四个角的扫描线
			Bitmap finalBitmap = mergeBitmap(bitmap, icon2);

			// 设置给view展示
			imgs.setImageBitmap(finalBitmap);

			// 及时回收不要的图片
			if (icon != null & !icon.isRecycled()) {
//				删
				icon2.recycle();
				icon.recycle();
				bitmap.recycle();
//				置null
				icon2=null;
				icon=null;
//				通知回收
				System.gc();
			}

		} catch (WriterException e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * 按照固定长宽 缩放图片 方法名:scaleBitmap
	 * 
	 * @param 需要缩放的图片
	 * @param newWidth
	 *            缩放后的宽度
	 * @param newHeight
	 *            缩放后的长度
	 * @return 新的图片
	 */
	public Bitmap scaleBitmap(Bitmap bm, int newWidth, int newHeight) {
		// 获得图片的宽高
		int width = bm.getWidth();
		int height = bm.getHeight();
		// 计算缩放比例
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的缩放后的图片
		Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
				true);
		return newbm;
	}
	/**
	 * 合并2张图 方法名:mergeBitmap
	 * 
	 * @param src
	 *            背景图
	 * @param icon
	 *            需要画在中间的icon
	 * @return 合成后的图片
	 */
	public Bitmap mergeBitmap(Bitmap src, Bitmap icon) {
		if (src == null) {
			return null;
		}
		int w = src.getWidth();
		int h = src.getHeight();
		int ww = icon.getWidth();
		int wh = icon.getHeight();
		// create the new blank bitmap
		Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		Canvas cv = new Canvas(newb);
		cv.drawBitmap(src, 0, 0, null);
		cv.drawBitmap(icon, (w - ww) / 2, (h - wh) / 2, null);
		cv.save(Canvas.ALL_SAVE_FLAG);
		cv.restore();
		return newb;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值