zxing java 加log_使用Zxing生成二維碼,並添加水印

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

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 MatrixToImageWriterEx {

private static final Log logger = LogFactory.getLog(EASWebserviceUtils2.class);

private static final int BLACK = 0xFF000000;

private static final int WHITE = 0xFFFFFFFF;

private static final int LogoPart = 4;

/**

* 生成二維碼前的配置信息

* @param content

* @param format

* @return

*/

public static BitMatrix createQRCode(String content,String format,int width,int hight){

Hashtable hints = new Hashtable();

hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //指定糾錯等級

BitMatrix bitMatrix=null;

try {

bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, hints);

} catch (WriterException e) {

logger.error("生成二維碼錯誤",e);

}

return bitMatrix;

}

/**

* 將logo圖片放到二維碼中間(水印效果)

* 將生成的圖片以流的形式輸出到頁面展示

* @param matrix

* @param format

* @param outStream

* @param logoPath

* @throws IOException

*/

public static void megerToFile(BitMatrix matrix,String format,OutputStream outStream,String logoPath) throws IOException {

BufferedImage image = toBufferedImage(matrix);

Graphics2D gs = image.createGraphics();

//載入logo圖片

BufferedImage img = ImageIO.read(new File(logoPath));

int width = image.getWidth() / LogoPart;

int height = image.getHeight() / LogoPart;

//logo起始位置,以便logo居中顯示

int x = (image.getWidth() - width) / 2;

int y = (image.getHeight() - height) / 2;

gs.drawImage(img, x, y, img.getWidth(), img.getHeight(), null);

gs.dispose();

img.flush();

ImageIO.write(image, format, outStream);

}

/**

* 將logo圖片放到二維碼中間(水印效果)

* 將二維碼圖片生成到某硬盤路徑下

* @param matrix

* @param format

* @param file

* @param logoPath

* @throws IOException

*/

public static void megerToFile2(BitMatrix matrix,String format,String imggePath,String logoPath) throws IOException {

BufferedImage image = toBufferedImage(matrix);

Graphics2D gs = image.createGraphics();

//載入logo圖片

BufferedImage img = ImageIO.read(new File(logoPath));

int width = image.getWidth() / LogoPart;

int height = image.getHeight() / LogoPart;

//logo居中顯示

int x = (image.getWidth() - width) / 2;

int y = (image.getHeight() - height) / 2;

gs.drawImage(img, x, y, img.getWidth(), img.getHeight(), null);

gs.dispose();

img.flush();

ImageIO.write(image, format, new File(imggePath));

}

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

for(int y=0;y

image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);

}

}

return image;

}

}

2、測試類

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值