二维码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开源包。生成二维码。

原文地址:https://www.cnblogs.com/jingRegina/p/5500601.html