效果图如下:
制作过程很简单的就是在原始的二维码图片上添加一个logn图标,代码的注释写得很详细,也就不给大家啰嗦了
package com.example.day44_02_qrcodewithlogin;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class MainActivity extends Activity {
private ImageView imageView_main ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
imageView_main = (ImageView) findViewById(R.id.imageView_main);
String content = "我们要毕业了";
createQRCode(content,500,500);
}
/**
* 用于创建一个二维码
* @param content
* @param width
* @param height
*/
private void createQRCode(String content, int width, int height) {
try {
//1,创建实例化对象
QRCodeWriter writer = new QRCodeWriter() ;
//2,设置字符集
Map<EncodeHintType, String> map = new HashMap<EncodeHintType, String>();
map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//3,通过encode方法将内容写入矩阵对象
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height,map);
//4,定义一个二维码像素点的数组,向每个像素点中填充颜色
int[] pixels = new int[width*height];
//5,往每一像素点中填充颜色(像素没数据则用黑色填充,没有则用彩色填充,不过一般用白色)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (matrix.get(j, i)) {
pixels[i*width+j] = 0xff000000;
}else {
pixels[i*width+j] = 0xffffffff;
}
}
}
//6,创建一个指定高度和宽度的空白bitmap对象
Bitmap bm_QR = Bitmap.createBitmap(width, height, Config.ARGB_8888);
//7,将每个像素的颜色填充到bitmap对象
bm_QR.setPixels(pixels, 0, width, 0, 0, width, height);
//8,创建一个bitmap对象用于作为其图标
Bitmap bm_login = BitmapFactory.decodeResource(getResources(), R.drawable.img_kf_qq);
//9,创建一个方法在二维码上添加一张图片
if (addLogin(bm_QR,bm_login) != null) {
imageView_main.setImageBitmap(addLogin(bm_QR,bm_login));
}
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 用于向创建的二维码中添加一个logn
* @param bm_QR
* @param bm_login
* @return
*/
private Bitmap addLogin(Bitmap bm_QR, Bitmap bm_login) {
if (bm_QR == null) {
return null;
}
if (bm_login == null) {
return bm_QR ;
}
//获取图片的宽高
int bm_QR_Width = bm_QR.getWidth() ;
int bm_QR_Height = bm_QR.getHeight();
int bm_login_Width = bm_login.getWidth() ;
int bm_login_Height = bm_login.getHeight();
//设置logn的大小为二维码整体大小的1/5
float scale_login = bm_QR_Width*1.0f /5/bm_login_Width ;
Bitmap bitmap = Bitmap.createBitmap(bm_QR_Width, bm_QR_Height, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bm_QR, 0, 0, null);
canvas.scale(scale_login, scale_login, bm_QR_Width / 2, bm_QR_Height / 2);
canvas.drawBitmap(bm_login, (bm_QR_Width - bm_login_Width) / 2, (bm_QR_Height - bm_login_Height) / 2, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}