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>
運行效果:
總結(jié)
以上所述是小編給大家介紹的Android生成條形碼和二維碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Android程序開發(fā)之Listview下拉刷新上拉(滑動分頁)加載更多
這篇文章主要介紹了Android程序開發(fā)之Listview下拉刷新上拉(滑動分頁)加載更多的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Android開發(fā)實現(xiàn)保存圖片到手機相冊功能
這篇文章主要介紹了Android開發(fā)實現(xiàn)保存圖片到手機相冊功能,涉及Android圖形及文件相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android自定義view實現(xiàn)電影票在線選座功能
這篇文章主要為大家詳細介紹了Android自定義view實現(xiàn)選座功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android Animation之TranslateAnimation(平移動畫)
這篇文章主要為大家詳細介紹了Animation之TranslateAnimation平移動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09深入Understanding Android ContentProvider詳解
本篇文章是對Android ContentProvider進行了詳細的分析介紹,需要的朋友參考下2013-05-05Ubuntu中為Android增加硬件抽象層(HAL)模塊訪問Linux內(nèi)核驅(qū)動程序
本文主要介紹在Ubuntu上為Android HAL模塊訪問Linux內(nèi)核驅(qū)動程序,這里給大家提供方法和一個小的測試程序代碼,以及常遇到的問題和解決方法,有需要的小伙伴可以參考下2016-08-08Android檢測手機中存儲卡及剩余空間大小的方法(基于Environment,StatFs及DecimalFormat
這篇文章主要介紹了Android檢測手機中存儲卡及剩余空間大小的方法,基于Environment,StatFs及DecimalFormat實現(xiàn)該功能,具有一定參考借鑒價值,需要的朋友可以參考下2016-01-01詳解android使用ItemDecoration 懸浮導航欄效果
本篇文章主要介紹了Android 最流行的吸頂效果的實現(xiàn)及代碼,非常具有實用價值,需要的朋友可以參考下。2017-01-01