Android利用zxing生成二維碼的過程記錄
二維碼生成原理(即工作原理)
二維碼官方叫版本Version。Version 1是21 x 21的矩陣,Version 2是 25 x 25的矩陣,Version 3是29的尺寸,每增加一個version,就會增加4的尺寸,公式是:(V-1)*4 + 21(V是版本號) 最高Version 40,(40-1)*4+21 = 177,所以最高是177 x 177 的正方形。
下面是一個二維碼的樣例:

效果圖如下:

前提:
導(dǎo)入 zxing 的 jar 后開始操作,老規(guī)矩最后有源碼,作者布局默認相對布局。
第一步:定義二維碼的長寬高及圖片控件

第二步:實例化 QRCodeWriter 后利用 for 循環(huán)將二維碼畫出來,然后用圖片控件加載圖片。

源碼如下:
布局文件:**
<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="點擊顯示二維碼"
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="請輸入要加載成二維碼的內(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("請輸入內(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"); //記得要自定義長寬
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)將要表示的信息寫出來
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android上使用ZXing識別條形碼與二維碼的方法
- Android中使用ZXing生成二維碼(支持添加Logo圖案)
- Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描
- Android利用ZXing掃描二維碼的實例代碼解析
- Android基于google Zxing實現(xiàn)二維碼的生成
- Android實現(xiàn)基于ZXing快速集成二維碼掃描功能
- Android 超簡易Zxing框架 生成二維碼+掃碼功能
- Android Zxing二維碼掃描圖片拉伸問題的解決方法
- Android中利用zxing實現(xiàn)自己的二維碼掃描識別詳解
- Android Zxing二維碼掃描圖片拉伸的解決方法
相關(guān)文章
Android開發(fā)Jetpack組件Lifecycle原理篇
這一篇文章來介紹Android?Jetpack架構(gòu)組件的Lifecycle;?Lifecycle用于幫助開發(fā)者管理Activity和Fragment?的生命周期,?由于Lifecycle是LiveData和ViewModel的基礎(chǔ);所以需要先學(xué)習(xí)它2022-08-08
Android控件ImageSwitcher實現(xiàn)左右圖片切換功能
這篇文章主要為大家詳細介紹了Android控件ImageSwitcher實現(xiàn)左右圖片切換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
Android 關(guān)閉多個Activity的實現(xiàn)方法
這篇文章主要介紹了Android 關(guān)閉多個Activity的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android實現(xiàn)熱門標(biāo)簽的流式布局
這篇文章主要介紹了Android實現(xiàn)熱門標(biāo)簽的流式布局的詳細方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12

