使用Zxing生成二维码,并添加水印

来源:互联网 发布:程序员的自我修养 书签 编辑:程序博客网 时间:2024/05/21 13:55

简单通过Zxing实现二维码图片中间添加水印功能,即实现以下效果:



1、二维码生成工具类
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<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();           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<width;x++){              for(int y=0;y<height;y++){                  image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);              }          }          return image;         }        }
2、测试类
import java.io.IOException;import com.google.zxing.common.BitMatrix;public class Test {public static void main(String[] args) throws IOException {String content="http://www.baidu.com";BitMatrix bitMatrix = MatrixToImageWriterEx.createQRCode(content, "jpg", 300, 300);MatrixToImageWriterEx.megerToFile2(bitMatrix, "jpg", "D:\\img\\showImg.jpg", "D:\\img\\logo.jpg");}}
0 0