欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Android

當前位置:主頁 > 軟件編程 > Android >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Android|點擊: 次

背景:

        隨著移動互聯(lián)網(wǎng)的普及以及智能終端設備的廣泛應用,移動支付變得越來越便捷,通過掃描二維碼代替?zhèn)鹘y(tǒng)的刷卡行為。那么作為開發(fā)者而言生成二維碼成為了一項必備技能。

準備:

        使用zxing包

        implementation "com.google.zxing:core:3.3.1"

核心代碼:

package com.wangpengpro.h5test.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.java
public 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>

運行效果:

總結

以上所述是小編給大家介紹的Android生成條形碼和二維碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

上一篇:RecyclerView+SnapHelper實現(xiàn)無限循環(huán)篩選控件

欄    目:Android

下一篇:Android 自定義驗證碼輸入框的實例代碼(支持粘貼連續(xù)性)

本文標題:Android生成條形碼和二維碼功能

本文地址:http://mengdiqiu.com.cn/a1/Android/9149.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有