Android利用zxing生成二維碼的過(guò)程記錄
二維碼生成原理(即工作原理)
二維碼官方叫版本Version。Version 1是21 x 21的矩陣,Version 2是 25 x 25的矩陣,Version 3是29的尺寸,每增加一個(gè)version,就會(huì)增加4的尺寸,公式是:(V-1)*4 + 21(V是版本號(hào)) 最高Version 40,(40-1)*4+21 = 177,所以最高是177 x 177 的正方形。
下面是一個(gè)二維碼的樣例:
效果圖如下:
前提:
導(dǎo)入 zxing 的 jar 后開始操作,老規(guī)矩最后有源碼,作者布局默認(rèn)相對(duì)布局。
第一步:定義二維碼的長(zhǎng)寬高及圖片控件
第二步:實(shí)例化 QRCodeWriter 后利用 for 循環(huán)將二維碼畫出來(lái),然后用圖片控件加載圖片。
源碼如下:
布局文件:**
<Button android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="0dp" android:text="點(diǎn)擊顯示二維碼" android:textSize="20sp" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="192dp" android:src="@drawable/ic_launcher_background" /> <EditText android:id="@+id/myeditText" android:layout_width="300dp" android:maxLines="1" android:layout_height="wrap_content" android:layout_below="@+id/mybutton" android:layout_centerHorizontal="true" android:ems="10" android:hint="請(qǐng)輸入要加載成二維碼的內(nèi)容" />
java 文件:
public class MainActivity extends Activity implements View.OnClickListener { private int width = 300; private int height = 300; private ImageView imageView; private Bitmap bit; private Button mybutton; private EditText myeditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { imageView = (ImageView) findViewById(R.id.imageView); mybutton = (Button) findViewById(R.id.mybutton); mybutton.setOnClickListener(this); myeditText = (EditText) findViewById(R.id.myeditText); myeditText.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.mybutton: String name=myeditText.getText().toString(); if(name.equals("")){ myeditText.setError("請(qǐng)輸入內(nèi)容"); }else{ zxing(name); } break; } } private void zxing(String name){ QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //記得要自定義長(zhǎng)寬 BitMatrix encode = null; try { encode = qrCodeWriter.encode(name, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { e.printStackTrace(); } int[] colors = new int[width * height]; //利用for循環(huán)將要表示的信息寫出來(lái) for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (encode.get(i, j)) { colors[i * width + j] = Color.BLACK; } else { colors[i * width + j] = Color.WHITE; } } } bit = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565); imageView.setImageBitmap(bit); } }
總結(jié)
到此這篇關(guān)于Android利用zxing生成二維碼的文章就介紹到這了,更多相關(guān)Android zxing生成二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android上使用ZXing識(shí)別條形碼與二維碼的方法
- Android中使用ZXing生成二維碼(支持添加Logo圖案)
- Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描
- Android利用ZXing掃描二維碼的實(shí)例代碼解析
- Android基于google Zxing實(shí)現(xiàn)二維碼的生成
- Android實(shí)現(xiàn)基于ZXing快速集成二維碼掃描功能
- Android 超簡(jiǎn)易Zxing框架 生成二維碼+掃碼功能
- Android Zxing二維碼掃描圖片拉伸問(wèn)題的解決方法
- Android中利用zxing實(shí)現(xiàn)自己的二維碼掃描識(shí)別詳解
- Android Zxing二維碼掃描圖片拉伸的解決方法
相關(guān)文章
Android開發(fā)Jetpack組件Lifecycle原理篇
這一篇文章來(lái)介紹Android?Jetpack架構(gòu)組件的Lifecycle;?Lifecycle用于幫助開發(fā)者管理Activity和Fragment?的生命周期,?由于Lifecycle是LiveData和ViewModel的基礎(chǔ);所以需要先學(xué)習(xí)它2022-08-08Android接收和發(fā)送短信的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android接收和發(fā)送短信的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-08-08Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
這篇文章主要為大家詳細(xì)介紹了Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05詳解Android壁紙服務(wù)的啟動(dòng)過(guò)程
你有設(shè)置過(guò)手機(jī)的壁紙嗎,你知道壁紙是什么樣的程序它是怎么在后臺(tái)運(yùn)行的嗎?這篇文章主要介紹了詳解Android系統(tǒng)壁紙服務(wù)的啟動(dòng)過(guò)程2021-08-08Android 關(guān)閉多個(gè)Activity的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 關(guān)閉多個(gè)Activity的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09Android Studio配置反混淆的實(shí)現(xiàn)
這篇文章主要介紹了Android Studio如何混淆的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android實(shí)現(xiàn)熱門標(biāo)簽的流式布局
這篇文章主要介紹了Android實(shí)現(xiàn)熱門標(biāo)簽的流式布局的詳細(xì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12