zxing实现带logo的二维码图片生成功能实现
public class CreateQRCodeImg {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
public static BufferedImage toBufferedImage(BitMatrix matrix){
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix);
Graphics2D gs = image.createGraphics();
//载入logo
Image img = ImageIO.read(new File(logoPath));
gs.drawImage(img, 125, 125, null);
gs.dispose();
img.flush();
if(!ImageIO.write(image, format, file)){
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 最终调用该方法生成二维码图片
* @param url
* @param imgPath 二维码生成的绝对路径
* @param logoPath 二维码中间logo绝对地址
* @throws Exception
*/
public static void get2CodeImage(String url,String imgPath,String logoPath) throws Exception{
String format = "png";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);
File outputFile = new File(imgPath);
CreateQRCodeImg.writeToFile(bitMatrix, format, outputFile,logoPath);
}
}
PS:zxing相关jar可到网上搜寻,或者在本人资源分享里面下载!
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
public static BufferedImage toBufferedImage(BitMatrix matrix){
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix);
Graphics2D gs = image.createGraphics();
//载入logo
Image img = ImageIO.read(new File(logoPath));
gs.drawImage(img, 125, 125, null);
gs.dispose();
img.flush();
if(!ImageIO.write(image, format, file)){
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 最终调用该方法生成二维码图片
* @param url
* @param imgPath 二维码生成的绝对路径
* @param logoPath 二维码中间logo绝对地址
* @throws Exception
*/
public static void get2CodeImage(String url,String imgPath,String logoPath) throws Exception{
String format = "png";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);
File outputFile = new File(imgPath);
CreateQRCodeImg.writeToFile(bitMatrix, format, outputFile,logoPath);
}
}
PS:zxing相关jar可到网上搜寻,或者在本人资源分享里面下载!
...全文
相关推荐
使用Java zxing开发的二维码 带logo彩色二维码
使用google开源zxing工具自己整理的二维码编码和解析代码,将logo按比例压缩后添加至彩色二维码中。
使用zxing开发的二维码/条形码/带logo彩色二维码例子
使用google开源zxing工具自己整理的二维码和条形码成功和解析代码, 新增加了生成带logo小图片的彩色二维码例子。 附加图片: 把img下面的图片放到c盘下做测试用
java二维码工具类,中间带LOGO的,很强大
java二维码工具类,中间带LOGO的,很强大
java生成中间带logo的二维码图片(源码)
用java生成一个二维码图片,中间带一个logo,源码,直接用就可以。
java生成二维码工具类(带logo和文字说明) 很实用
java 使用zxing生成二维码(带logo和文字说明)已经封装 使用起来很方便 下载下来直接调用传入参数就可以


