国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Android如何生成條形碼和二維碼功能

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(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í),將立刻刪除涉嫌侵權內容。

精品一区二区WW| АV天堂手机版在线观看| 亚洲综合久久成人AV| 女人自熨全过程直播| 成在人线AV无码免费看网站直播| 玩弄CHINESE丰满人妻VIDEOS|