二维码QRCode
Posted sunshineicy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二维码QRCode相关的知识,希望对你有一定的参考价值。
package com.aig.ecompass.ecard; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; public class EcardQRCode { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; public static void createQRCode(){ //WebSphere Application Server v8.0 at localhost String content = "AIA Technology Shared Service,ECard QRCode"; String path = "D:/"; String suffix="png"; try { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints); File file = new File(path,"ecard.png"); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); } } if (!ImageIO.write(image, suffix, file)) { throw new IOException("Could not write an image of format " + suffix + " to " + file); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { createQRCode(); } }
使用zxing的core.jar包,zxing为Google开源包。生成二维码。
以上是关于二维码QRCode的主要内容,如果未能解决你的问题,请参考以下文章