您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Android如何生成條形碼和二維碼功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
背景:
隨著移動(dòng)互聯(lián)網(wǎng)的普及以及智能終端設(shè)備的廣泛應(yīng)用,移動(dòng)支付變得越來越便捷,通過掃描二維碼代替?zhèn)鹘y(tǒng)的刷卡行為。那么作為開發(fā)者而言生成二維碼成為了一項(xiàng)必備技能。
準(zhǔn)備:
使用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>
感謝各位的閱讀!關(guān)于“Android如何生成條形碼和二維碼功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。