package cn.qrcode; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import cn.utils.ImageUtils; 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.decoder.ErrorCorrectionLevel; /** * 仿新浪微博二维码 */ public class TestZXing{ private static final int BLACK = 0xff000000; private static final int WHITE = 0xFFFFFFFF; /** 生成二维码图片大小 */ private static final int QRCODE_SIZE = 300; /** 头像图片大小 */ private static final int PORTRAIT_SIZE = 55; /** 头像图片 */ private BufferedImage portrait; private void onCreate() { // 建立二维码 BufferedImage qr = createQRCodeBitmap(); createQRCodeBitmapWithPortrait(qr); } /** * 初始化头像图片 */ private BufferedImage initProtrait(String url) { BufferedImage image = null; try { image = ImageIO.read(new File(url)); } catch (IOException e) { e.printStackTrace(); } // 对原有图片压缩显示大小 return ImageUtils.zoomInImage(image, 5); } /** * 创建QR二维码图片 */ private BufferedImage createQRCodeBitmap() { // 用于设置QR二维码参数 Hashtable<EncodeHintType, Object> qrParam = new Hashtable<EncodeHintType, Object>(); // 设置QR二维码的纠错级别――这里选择最高H级别 qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置编码方式 qrParam.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设定二维码里面的内容,这里我采用我微博的地址 String content = "sinaweibo://userinfo?uid=123456789"; // 生成QR二维码数据 try { // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, qrParam); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); // 创建一张bitmap图片 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // 开始利用二维码数据创建BufferedImage图片,分别设为黑白两色 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // 将上面的二维码颜色数组传入,生成图片颜色 image.setRGB(x, y, bitMatrix.get(x, y) == true ? BLACK : WHITE); } } return image; } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 在二维码上绘制头像 * @param image */ private void createQRCodeBitmapWithPortrait(BufferedImage image) { // 头像图片的大小 int width = image.getWidth() / 5; int height = image.getHeight() / 5; //头像 portrait = initProtrait("src/boy.jpg"); // 设置头像要显示的位置,即居中显示 int x = (QRCODE_SIZE - width) / 2; int y = (QRCODE_SIZE - height) / 2; Graphics2D g = image.createGraphics(); g.drawImage(portrait, x, y, width, height, null); g.dispose(); try { ImageIO.write(image, "png", new File("src/boy_br.png")); } catch (IOException e) { e.printStackTrace(); } } /** * * @param args */ public static void main(String[] args) { TestZXing tz = new TestZXing(); tz.onCreate(); System.out.println("创建成功!"); } }

zdmabc LV3
2024年6月27日
xuexizhuanyong23 LV16
2022年3月23日
yangctz LV25
2021年9月29日
yiangeee LV9
2021年1月14日
abcd11112 LV6
2020年8月12日
hanweinan6 LV13
2020年6月29日
huasir2018 LV14
2020年5月9日
霸道小盆友 LV3
2020年4月30日
sonofy123 LV9
2020年2月14日
kong.yee LV40
2020年2月1日