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、測試類