二維碼生成前後端
阿新 • • 發佈:2018-11-11
二維碼(Quick Response Code),又稱二維條碼,它是用特定的幾何圖形按一定規律在平面(二維方向)上分佈的黑白相間的圖形,是所有資訊資料的一把鑰匙。在現代商業活動中,可實現的應用十分廣泛,如:產品防偽/溯源、廣告推送、網站連結、資料下載、商品交易、定位/導航、電子商務應用、車輛管理、資訊傳遞等。如今智慧手機掃一掃(簡稱313)功能的應用使得二維碼更加普遍,隨著國內物聯網產業的蓬勃發展,更多的二維碼技術應用解決方案被開發,二維碼成為移動網際網路入口真正成為現實。
前端JQUERY生成方法:
依賴JS檔案: ↓ ↓ ↓
qrcode.js,jquery.qrcode.js(點選下載)
生成二維碼頁面位置插入如下div:
<div id="code"></div>
頁面載入程式碼:
<script>
var id="${param.qrcode}";
if(id!=""&&id!=null){
$("#code").qrcode({
render: "table", //table方式
width: 200, //寬度
height:200, //高度
text: id //任意內容
});
}
</script>
後臺GOOGLE ZXING生成方法:
生成本地JPG二維碼圖片:
import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; public class test { public static void main(String[] args){ try { String content ="www.centit.com"; String path = "C:/"; MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints); File file1 = new File(path,"二維碼.jpg"); MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1); } catch (Exception e) { e.printStackTrace(); } }
MatrixToImageWriter.java
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
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;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}
依賴JRA包:core-2.0.jar,SOURCE:core-2.0-sources.jar
POM:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.0</version>
</dependency>