以前寫過一篇不帶logo的二維碼實現方式,採用QRCode和ZXing兩種方式
http://blog.csdn.net/xiaokui_wingfly/article/details/39476185
這裏介紹一下ZXing的帶logo實現方式,詳細實現參考一下代碼,測試使用ZXingCodeUtil的main方法。
視頻連接地址:http://www.tudou.com/programs/view/WSMfx1RL-ao/。視頻地址中包括圖片二維碼流輸出方式java
LogoConfig.javaapi
package com.weixin.pay.orcode.logo;
import java.awt.Color;
/** * logo背景配置 * * @author X-rapido */
public class LogoConfig {
public static final Color DEFAULT_BORDERCOLOR = Color.WHITE; // logo默認邊框顏色
public static final int DEFAULT_BORDER = 2; // logo默認邊框寬度
public static final int DEFAULT_LOGOPART = 5; // logo大小默以爲照片的1/5
private final int border = DEFAULT_BORDER; // 默認邊框寬度
private final Color borderColor; // 邊框顏色
private final int logoPart; // 邊框外圍寬度
/** * 二維碼無參構造函數 默認設置Logo圖片底色白色寬度2 */
public LogoConfig() {
this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
}
/** * 二維碼有參構造函數 * * @param borderColor 邊框顏色 * @param logoPart 邊框寬度 */
public LogoConfig(Color borderColor, int logoPart) {
// 設置邊框
this.borderColor = borderColor;
// 設置邊框寬度
this.logoPart = logoPart;
}
/** * 獲取邊框顏色 * * @return 獲取邊框顏色 */
public Color getBorderColor() {
return borderColor;
}
/** * 獲取邊框 * * @return 獲取邊框 */
public int getBorder() {
return border;
}
/** * 外圍邊寬 * * @return 外圍邊寬 */
public int getLogoPart() {
return logoPart;
}
}
BufferedImageLuminanceSource.javamarkdown
package com.weixin.pay.orcode.logo;
import com.google.zxing.LuminanceSource;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
/** * 二維碼解析使用類 * * @author X-rapido * */
public class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedImage image;
private final int left;
private final int top;
public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
}
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
}
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}
@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
}
}
ZXingConfig.javaide
函數
package com.weixin.pay.orcode.logo;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
/** * 二維碼配置信息 * * @author X-rapido * */
public class ZXingConfig {
private boolean logoFlg = false; // 是否加入Log圖片
private String content; // 二維碼編碼內容
private BarcodeFormat barcodeformat = BarcodeFormat.QR_CODE; //編碼類型
private int width = 200; // 生成圖片寬度
private int height = 200; // 生成圖片高度
private Map<EncodeHintType, ?> hints; // 設置參數
private String logoPath; // Logo圖片路徑
private String putPath; // 圖片輸出路徑
private LogoConfig LogoConfig; // logo圖片參數
/** * 獲取 是否加入Log圖片 * * @return logoFlg 是否加入Log圖片 */
public boolean isLogoFlg() {
return logoFlg;
}
/** * 設置 是否加入Log圖片 * * @param logoFlg 是否加入Log圖片 */
public void setLogoFlg(boolean logoFlg) {
this.logoFlg = logoFlg;
}
/** * 獲取 二維碼編碼內容 * * @return content 二維碼編碼內容 */
public String getContent() {
return content;
}
/** * 設置 二維碼編碼內容 * * @param content 二維碼編碼內容 */
public void setContent(String content) {
this.content = content;
}
/** * 獲取 編碼類型 * * @return barcodeformat 編碼類型 */
public BarcodeFormat getBarcodeformat() {
return barcodeformat;
}
/** * 設置 編碼類型 * * @param barcodeformat 編碼類型 */
public void setBarcodeformat(BarcodeFormat barcodeformat) {
this.barcodeformat = barcodeformat;
}
/** * 獲取 生成圖片寬度 * * @return width 生成圖片寬度 */
public int getWidth() {
return width;
}
/** * 設置 生成圖片寬度 * * @param width 生成圖片寬度 */
public void setWidth(int width) {
this.width = width;
}
/** * 獲取 生成圖片高度 * * @return height 生成圖片高度 */
public int getHeight() {
return height;
}
/** * 設置 生成圖片高度 * * @param height 生成圖片高度 */
public void setHeight(int height) {
this.height = height;
}
/** * 獲取 設置參數 * * @return hints 設置參數 */
public Map<EncodeHintType, ?> getHints() {
return hints;
}
/** * 設置 設置參數 * * @param hints 設置參數 */
public void setHints(Map<EncodeHintType, ?> hints) { this.hints = hints; } /** * 獲取 Logo圖片路徑 * * @return logoPath Logo圖片路徑 */ public String getLogoPath() { return logoPath; } /** * 設置 Logo圖片路徑 * * @param logoPath Logo圖片路徑 */ public void setLogoPath(String logoPath) { this.logoPath = logoPath; } /** * 獲取 圖片輸出路勁 * * @return putPath 圖片輸出路勁 */ public String getPutPath() { return putPath; } /** * 設置 圖片輸出路勁 * * @param putPath 圖片輸出路勁 */ public void setPutPath(String putPath) { this.putPath = putPath; } /** * 獲取 logoConfig * * @return logoConfig logoConfig */ public LogoConfig getLogoConfig() { return LogoConfig; } /** * 設置 logoConfig * * @param logoConfig logoConfig */ public void setLogoConfig(LogoConfig logoConfig) { LogoConfig = logoConfig; } }
ZXingCodeUtil.java工具
this
package com.weixin.pay.orcode.logo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.weixin.pay.orcode.ZXingConfig;
/** * 二維碼生成google zxing * * @author X-rapido * */
public class ZXingCodeUtil {
/** * 二維碼圖片加入Logo * * @param bim 圖片流 * @param logoPic Logo圖片物理位置 * @param logoConfig Logo圖片設置參數 * @throws Exception 異常上拋 */
private void addLogo_QRCode(BufferedImage bim, File logoPic, LogoConfig logoConfig) throws Exception {
try {
// 對象流傳輸
BufferedImage image = bim;
Graphics2D g = image.createGraphics();
// 讀取Logo圖片
BufferedImage logo = ImageIO.read(logoPic);
// 設置logo的大小,本人設置爲二維碼圖片的20%,因爲過大會蓋掉二維碼
int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image.getWidth() * 2 / 10) : logo.getWidth(null), heightLogo = logo
.getHeight(null) > image.getHeight() * 2 / 10 ? (image.getHeight() * 2 / 10) : logo.getWidth(null); // 計算圖片放置位置 // logo放在中心 int x = (image.getWidth() - widthLogo) / 2; int y = (image.getHeight() - heightLogo) / 2; // 開始繪製圖片 g.drawImage(logo, x, y, widthLogo, heightLogo, null); g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15); g.setStroke(new BasicStroke(logoConfig.getBorder())); g.setColor(logoConfig.getBorderColor()); g.drawRect(x, y, widthLogo, heightLogo); g.dispose(); logo.flush(); image.flush(); } catch (Exception e) { throw e; } } /** * 二維碼的解析 * * @param image 圖片文件流 * @return 解析後的Result結果集 * @throws Exception 錯誤異常上拋 */ @SuppressWarnings("unchecked") public Result parseQR_CODEImage(BufferedImage image) throws Exception { // 設置解析 Result result = null; try { MultiFormatReader formatReader = new MultiFormatReader(); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); @SuppressWarnings("rawtypes") Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); result = formatReader.decode(binaryBitmap, hints); System.out.println("resultFormat = " + result.getBarcodeFormat()); System.out.println("resultText = " + result.getText()); } catch (Exception e) { throw e; } return result; } /** * 生成二維碼bufferedImage圖片 * @param zxingconfig 二維碼配置信息 * @return 生成後的 BufferedImage * @throws Exception 異常上拋 */ public BufferedImage getQR_CODEBufferedImage(ZXingConfig zxingconfig) throws Exception { // Google配置文件 MultiFormatWriter multiFormatWriter = null; BitMatrix bm = null; BufferedImage image = null; try { multiFormatWriter = new MultiFormatWriter(); // 參數順序分別爲:編碼內容。編碼類型,生成圖片寬度,生成圖片高度,設置參數 bm = multiFormatWriter.encode(zxingconfig.getContent(), zxingconfig.getBarcodeformat(), zxingconfig.getWidth(), zxingconfig.getHeight(), zxingconfig.getHints()); int w = bm.getWidth(); int h = bm.getHeight(); image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); // 開始利用二維碼數據建立Bitmap圖片,分別設爲黑白兩色 for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { image.setRGB(x, y, bm.get(x, y) ?ui
Color.BLACK.getRGB() : Color.WHITE.getRGB()); } } // 是否設置Logo圖片 if (zxingconfig.isLogoFlg()) { this.addLogo_QRCode(image, new File(zxingconfig.getLogoPath()), zxingconfig.getLogoConfig()); } } catch (WriterException e) { throw e; } return image; } /** * 設置二維碼的格式參數 * * @return */ public Map<EncodeHintType, Object> getDecodeHintType() { // 用於設置QR二維碼參數 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 設置QR二維碼的糾錯級別(H爲最高級別)詳細級別信息 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 設置編碼方式 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN, 0); hints.put(EncodeHintType.MAX_SIZE, 350); hints.put(EncodeHintType.MIN_SIZE, 100); return hints; } /** * 這個工具類 ZXingCodeUtil 是提供二維碼圖片生成的工具 首先使用的時候需要實例化 * ZXingCodeUtil 而後實例化參數 ZXingConfig 和 LogoConfig 經過如下的演示可以詳細看參數是依照什麼循序進行設置 最後調用 * ZXingCodeUtil 中方法 getQR_CODEBufferedImage來生成二維碼 */ public static void main(String[] args) throws WriterException { String content = "http://www.baidu.com"; System.out.println("inputParam:" + content); try { // 生成二維碼 File file = new File("D:/Michael_QRCode.png"); ZXingCodeUtil zp = new ZXingCodeUtil(); // 實例化二維碼工具 ZXingConfig zxingconfig = new ZXingConfig(); // 實例化二維碼配置參數 zxingconfig.setHints(zp.getDecodeHintType()); // 設置二維碼的格式參數 zxingconfig.setContent(content);// 設置二維碼生成內容 zxingconfig.setLogoPath("D:/logo.jpg"); // 設置Logo圖片 zxingconfig.setLogoConfig(new LogoConfig()); // Logo圖片參數設置 zxingconfig.setLogoFlg(true); // 設置生成Logo圖片 BufferedImage bim = zp.getQR_CODEBufferedImage(zxingconfig);// 生成二維碼 ImageIO.write(bim, "png", file); // 圖片寫出 Thread.sleep(500); // 緩衝 zp.parseQR_CODEImage(bim); // 解析調用 } catch (Exception e) { e.printStackTrace(); } } }
資源Demo實例,點擊二維碼本身主動切換驗證碼,下載連接:
http://download.csdn.net/detail/xiaokui_wingfly/8846895
google