- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Android如何生成條形碼和二維碼功能
這篇文章給大家分享的是有關(guān)Android如何生成條形碼和二維碼功能的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
背景:
隨著(zhù)移動(dòng)互聯(lián)網(wǎng)的普及以及智能終端設備的廣泛應用,移動(dòng)支付變得越來(lái)越便捷,通過(guò)掃描二維碼代替傳統的刷卡行為。那么作為開(kāi)發(fā)者而言生成二維碼成為了一項必備技能。
準備:
使用zxing包
implementation "com.google.zxing:core:3.3.1"
核心代碼:
package com.wangpengpro.h6test.utils;import android.graphics.Bitmap;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;import java.util.HashMap;import java.util.Map;/** * @author Created by Mr.Wang on 2019/10/10 15:05. * usage: */public class CodeUtils { /** * 生成條形碼(不支持中文) * * @param content * @return */ public static Bitmap createBarcode(String content) { try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, 3000, 700); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 生成二維碼 * * @param content * @return */ public static Bitmap createQrcode(String content) { Map<EncodeHintType, Object> hints = new HashMap<>(); // 支持中文配置 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 1000, 1000 , hints); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }}
使用:
ImageActivity.javapublic class ImageActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); ImageView ivBarcode = findViewById(R.id.iv_barcode); ImageView ivQrcode = findViewById(R.id.iv_qrcode); ivBarcode.setImageBitmap(CodeUtils.createBarcode("This is a barcode")); ivQrcode.setImageBitmap(CodeUtils.createQrcode("This is a qrcode")); }}
activity_image.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ImageActivity"> <ImageView android:id="@+id/iv_barcode" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_qrcode" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站