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 的正方形。
下面是一個二維碼的樣例:

效果圖如下:

前提:
導入 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="請輸入要加載成二維碼的內容" />
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("請輸入內容");
}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);
}
}
總結
到此這篇關于Android利用zxing生成二維碼的文章就介紹到這了,更多相關Android zxing生成二維碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android 3D滑動菜單完全解析 Android實現推拉門式的立體特效
這篇文章主要為大家詳細介紹了Android 3D滑動菜單,Android實現推拉門式的立體特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android 布局中的android:onClick的使用方法總結
這篇文章主要介紹了Android 布局中的android:onClick的使用方法總結的相關資料,設置點擊時從上下文中調用指定的方法,這里提供實例幫助大家理解這部分內容,需要的朋友可以參考下2017-08-08
Android XMPP通訊自定義Packet&Provider
這篇文章主要介紹了Android XMPP通訊自定義Packet&Provider的相關資料,需要的朋友可以參考下2016-08-08
Android SharedPreferences實現保存登錄數據功能
這篇文章主要為大家詳細介紹了Android SharedPreferences實現保存登錄數據功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05

