一、生成二維碼的app
對於需要頻繁使用二維碼生成功能的人來說,下載一個二維碼生成的app是一個不錯的選擇。其中,zxing是一款非常優秀的二維碼生成app。
使用zxing生成二維碼的步驟十分簡單:
compile 'com.google.zxing:core:3.3.0'
導入依賴的庫後,在代碼中進行調用:
//生成二維碼圖片,並返回其bitmap對象
public Bitmap createQRCodeBitmap(String content) {
//定義二維碼參數
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
try {
//生成二維碼矩陣
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
//將二維碼矩陣轉換為bitmap對象,並返回
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bitmap.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
其中,參數”content”代表著二維碼中包含的內容,可以是鏈接、文字等等。
二、生成二維碼在線生成
在線生成二維碼的方式適合於只需要偶爾使用二維碼生成功能的用戶。zxing也提供了在線生成二維碼的功能網站,我們只需要在網站上輸入要生成的內容,就可以獲得相應的二維碼圖片。
網址是:http://zxing.net/qrcode/generator
在這個網站上,我們可以選擇二維碼的大小、顏色、後景顏色、容錯率等等。
三、生成二維碼在哪裡找
除了使用手機app或者在線生成,我們也可以直接在其他的應用程序中找到二維碼生成的功能。例如,微信的掃一掃功能就包含了二維碼生成的功能。
具體使用步驟如下:
- 在微信中打開掃一掃,並選擇「我的二維碼」
- 在彈出的二維碼頁面中輸入需要生成的內容,並選擇生成的樣式
- 點擊生成
- 長按生成的二維碼,選擇保存圖片即可
四、二維碼生成器
除了zxing之外,還有許多優秀的二維碼生成器可供使用。
其中,QR Code Generator就是一個不錯的選擇。它支持多種不同的二維碼類型,支持多種不同的內容格式,可以自定義二維碼顏色、大小等等。
網址是:https://www.qr-code-generator.com/
五、二維碼生成
二維碼生成基本的原理就是將需要轉化的數據使用演算法編碼成為一個二維碼矩陣,然後使用指定的樣式進行渲染。生成的二維碼矩陣包含了所需要的數據
下面是一個基本的例子,在控制台中列印二維碼的矩陣信息:
//生成二維碼矩陣
BitMatrix bitMatrix = new QRCodeWriter().encode("https://www.baidu.com", BarcodeFormat.QR_CODE, 300, 300, hints);
for (int y = 0; y < bitMatrix.getHeight(); y++) {
for (int x = 0; x < bitMatrix.getWidth(); x++) {
System.out.print(bitMatrix.get(x, y) ? "1" : "0");
}
System.out.println();
}
六、二維碼生成版
二維碼生成版是一個較為完整的二維碼生成工具。
具有以下特點:
- 支持多種不同的二維碼類型
- 支持多種不同的內容格式
- 支持自定義二維碼顏色、大小、前景色、後景色等等
- 支持批量生成二維碼
- 支持將已經生成的二維碼進行導出
七、二維碼生成活碼
二維碼生成活碼是指將一個二維碼與某些信息進行關聯,並且可以動態更新這些信息。
下面是一個使用二維碼生成活碼的例子:
//使用二維碼生成活碼
String content = "http://www.baidu.com";
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
//創建數據
Map<String, Object> data = new HashMap<>();
data.put("id", 1);
data.put("username", "張三");
data.put("message", "您的訂單已經發貨。");
//將數據關聯到二維碼上,並返回動態二維碼圖片地址
String qrcodeUrl = QRCodeUtils.generateDynamicQRCode(content, data, 300, 300);
八、二維碼生成樣式
二維碼生成樣式是指指定二維碼的顏色、大小、前景色、後景色等等。
下面是一個例子:
//定義二維碼參數
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
//定義二維碼顏色,前景色,後景色
int QRcodeColor = 0xff000000;
int fillColor = 0xffff00ff;
int frameColor = 0xff00ff00;
//生成二維碼矩陣
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
for (int y = 0; y < bitMatrix.getHeight(); y++) {
for (int x = 0; x < bitMatrix.getWidth(); x++) {
if (bitMatrix.get(x, y)) {
canvas.drawPoint(x, y, mPaint);
} else {
canvas.drawPoint(x, y, mFramePaint);
}
}
}
九、生成的二維碼在哪裡
生成的二維碼在文件系統中,並且可以使用所在應用程序進行導出。
下面是一個導出二維碼的例子:
//將二維碼Bitmap對象保存到文件中,然後通知系統更新圖片媒體庫以便其他應用也能看到
public static void saveBitmapToFile(Context context, Bitmap bitmap, String fileName) {
if (bitmap == null) {
Log.e(TAG, "saveBitmapToFile: bitmap is null.");
return;
}
final File directory = context.getExternalFilesDir(Environment.DIRECTORY_DCIM);
if (!directory.exists()) {
directory.mkdirs();
}
final File file = new File(directory, fileName + ".jpg");
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
} catch (Exception e) {
Log.e(TAG, "saveBitmapToFile: ", e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
Log.e(TAG, "saveBitmapToFile: ", e);
}
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/196930.html