package com.fanxin.huangfangyi.main.activity; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.widget.ImageView; import com.alibaba.fastjson.JSONObject; import com.fanxin.huangfangyi.DemoApplication; import com.fanxin.huangfangyi.R; import com.fanxin.huangfangyi.ui.BaseActivity; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class MyQrActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fx_activity_qrcode_generate); String key = "userInfo:"; JSONObject jsonObject=DemoApplication.getInstance().getUserJson(); jsonObject.remove("friends"); String value = jsonObject.toJSONString(); Bitmap qrcode = generateQRCode(key+value); ImageView imageView = (ImageView) findViewById(R.id.code_image); imageView.setImageBitmap(qrcode); } private Bitmap generateQRCode(String qrCodeString){ Bitmap bmp = null; //二维码图片 QRCodeWriter writer = new QRCodeWriter(); try { BitMatrix bitMatrix = writer.encode(qrCodeString, BarcodeFormat.QR_CODE, 512, 512); //参数分别表示为: 条码文本内容,条码格式,宽,高 int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); //绘制每个像素 for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } } catch (WriterException e) { e.printStackTrace(); } return bmp; } }