依賴jar包
二維碼的實現有多種方法,比如 Google 的 zxing 和日本公司的 QrCode,本文以 QrCode 為例。
QrCode.jar:https://pan.baidu.com/s/1c1PYV0s
加入本地 maven:
mvn install:install-file -Dfile=QRCode.jar -DgroupId=QRCode -DartifactId=QRCode -Dversion=3.0 -Dpackaging=jar
注意,QrCode 實現的二維碼不能很好的支持中文。
實例源碼
import com.swetake.util.Qrcode; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /** * 1、QrCode 生成二維碼圖片 * 2、生成帶有 logo 的二維碼圖片 * Created by zhengbinMac on 2017/4/27. */ public class QrCodeTest { /** * 生成二維碼 Buffered * @param content 二維碼內容 * @return */ public static BufferedImage QrcodeImage(String content) { // 二維碼寬度 int width = 140; // 二維碼高度 int height = 140; try { Qrcode qrcode = new Qrcode(); // 設置二維碼的排錯率 'L':7%,'M':15%,'Q':25%,'H':30% qrcode.setQrcodeErrorCorrect('M'); qrcode.setQrcodeEncodeMode('B'); // 設置二維碼的尺寸,尺寸越大,可存儲的信息量越大 qrcode.setQrcodeVersion(7); // 設置圖片的尺寸、格式 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 繪制二維碼的圖片 Graphics2D graphics2D = bufferedImage.createGraphics(); // 設置背景顏色 graphics2D.setBackground(Color.WHITE); // 創建二維碼的矩形區域 graphics2D.clearRect(0, 0, width, height); // 設置二維碼圖片的顏色值 graphics2D.setColor(Color.BLACK); // 二維碼生成點陣的偏移量 int pixoff = 2; // 獲取二維碼內容的字節數組,並設置編碼 byte[] contentBytes = content.getBytes("UTF-8"); // 輸出二維碼 if (contentBytes.length > 0 && contentBytes.length < 200) { // 如果二維碼內容在規定長度內 boolean[][] codeOut = qrcode.calQrcode(contentBytes); for (int i = 0;i < codeOut.length;i++) { for (int j = 0;j < codeOut.length;j++) { if (codeOut[j][i]) { graphics2D.fillRect(j*3+pixoff, i*3+pixoff, 3, 3); } } } } graphics2D.dispose(); bufferedImage.flush(); return bufferedImage; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 在已有的二維碼圖片加上logo圖片 * @param twodimensioncodeImg 二維碼圖片文件 * @param logoImg logo圖片文件 * @return */ public static BufferedImage encodeImgLogo(File twodimensioncodeImg,File logoImg){ BufferedImage twodimensioncode = null; try{ if(!twodimensioncodeImg.isFile() || !logoImg.isFile()){ System.out.println("輸入非圖片"); return null; } //讀取二維碼圖片 twodimensioncode = ImageIO.read(twodimensioncodeImg); //獲取畫筆 Graphics2D g = twodimensioncode.createGraphics(); //讀取logo圖片 BufferedImage logo = ImageIO.read(logoImg); //設置二維碼大小,太大,會覆蓋二維碼,此處20% int logoWidth = logo.getWidth(null) > twodimensioncode.getWidth()*3 /10 ? (twodimensioncode.getWidth()*3 /10) : logo.getWidth(null); int logoHeight = logo.getHeight(null) > twodimensioncode.getHeight()*3 /10 ? (twodimensioncode.getHeight()*3 /10) : logo.getHeight(null); // 確定二維碼的中心位置坐標,設置logo圖片放置的位置 int x = (twodimensioncode.getWidth() - logoWidth) / 2; int y = (twodimensioncode.getHeight() - logoHeight) / 2; //開始合並繪制圖片 g.drawImage(logo, x, y, logoWidth, logoHeight, null); g.drawRoundRect(x, y, logoWidth, logoHeight, 15 ,15); //logo邊框大小 g.setStroke(new BasicStroke(2)); //logo邊框顏色 g.setColor(Color.WHITE); g.drawRect(x, y, logoWidth, logoHeight); g.dispose(); logo.flush(); twodimensioncode.flush(); }catch(Exception e){ System.out.println("二維碼繪制logo失敗"); } return twodimensioncode; } /** * 生成圖片文件 * @param bufferedImage 圖片 buffered * @param imgPath 圖片路徑 */ public static void writeImage(BufferedImage bufferedImage, String imgPath) { // 生成二維碼的圖片 File file = new File(imgPath); try { ImageIO.write(bufferedImage, "png", file); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // 生成二維碼 BufferedImage qrCode = QrcodeImage("http://zhengbin.cnblogs.com"); writeImage(qrCode, "/Users/zhengbinMac/Documents/qrcode/qecode2.png"); // 生成帶有圖片logo的二維碼 File qrcode = new File("/Users/zhengbinMac/Documents/qrcode/qecode2.png"); File logo = new File("/Users/zhengbinMac/Documents/qrcode/logo.png"); BufferedImage logoQrcode = encodeImgLogo(qrcode, logo); writeImage(logoQrcode, "/Users/zhengbinMac/Documents/qrcode/logQrcode.png"); } }
二維碼效果
下面打開微信掃一下吧
超鏈接二維碼: 普通文本內容二維碼:
注意,超鏈接前需要加 ' http:// ',否則顯示普通文本內容
二維碼的解析
/** * 解析二維碼 */ public static String decodeImg(String imgPath) { File imgFile = new File(imgPath); if (imgFile == null) { return null; } BufferedImage bufferedImage; String content = null; try { bufferedImage = ImageIO.read(imgFile); QRCodeDecoder decoder = new QRCodeDecoder(); // decode 方法入參為 QRCodeImage,其是接口聲明,需要實現該接口 content = new String(decoder.decode(new CodeImg(bufferedImage)), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return content; } /** * 實現 QRCodeImage 接口 */ static class CodeImg implements QRCodeImage{ private BufferedImage image; public CodeImg(BufferedImage image) { super(); this.image = image; } @Override public int getWidth() { return image.getWidth(); } @Override public int getHeight() { return image.getHeight(); } @Override public int getPixel(int x, int y) { return image.getRGB(x, y); } public BufferedImage getImage() { return image; } public void setImage(BufferedImage image) { this.image = image; } }