編輯:關於 Android 開發
參考資料:https://blog.csdn.net/books1958/article/details/46346531 與 https://www.cnblogs.com/mythou/p/3280023.html
網路上很多文章使用的都是舊版、已過時的做法,找了半天只有這兩篇比較好用。這裡使用的 ZXing 套件版本為 3.1。
MainActivity 程式碼如下:
package com.example.wang.bmap;
import com.google.zxing.*;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class MainActivity extends AppCompatActivity {
private Button genButton;
private ImageView img;
private EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
genButton = (Button) findViewById(R.id.gen);
img = (ImageView) findViewById(R.id.img);
input = (EditText) findViewById(R.id.input);
createQR();
}
public void createQR() {
genButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String in = input.getText().toString();
try {
BitMatrix qrcode = new QRCodeWriter().encode(in, BarcodeFormat.QR_CODE, 400, 400);
int[] pixels = new int[400 * 400];
for (int y = 0; y < 400; y++) {
for (int x = 0; x < 400; x++) {
if (qrcode.get(x, y)) {
pixels[y * 400 + x] = 0xff000000;
} else {
pixels[y * 400 + x] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, 400, 0, 0, 400, 400);
img.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
});
}
}
XML 佈局程式碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wang.bmap.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="從下邊生成二維碼" />
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="請輸入要生成的二維碼文字" />
<Button
android:id="@+id/gen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input"
android:layout_marginTop="10dp"
android:text="生成二維碼" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/gen"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
</RelativeLayout>
關於中文亂碼問題,網路上有各種答案,我採用的是透過 hints 指定 UTF-8 編碼:
Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix qrcode = new QRCodeWriter().encode(in, BarcodeFormat.QR_CODE, 400, 400, hints);
效果如圖:

Android MVP 架構最佳實踐
從 MVC 到 MVP Android 早期常以 MVC 形容架構,Activity 同時扮演 Controller 與 View 的角色。隨著業務複雜度提升,Activ
Android 開發實戰(十二):透一通訊錄資料庫,讀取手機與 SIM 卡聯絡人
手機裡的聯絡人並不是散落在某個檔案中,而是整整齊齊躺在系統資料庫裡。想拿到這些資料,就得學會跟系統的 ContentProvider 打交道。這一節先帶你把 Andro
Android JSON 資料解析與建構指南
JSON 簡介JSON 是一種輕量級資料交換格式,主要由兩種結構組成:名稱與值的配對,以及有序的值列表。前者通常對應物件,後者通常對應陣列。物件以左右大括號包圍,名稱與值
Android Studio 完整建置指南:從 JDK、SDK 到專案結構與快捷鍵
Android Studio 完整建置指南:從 JDK、SDK 到專案結構與快捷鍵 本文整理 Android Studio 的安裝、設定與日常使用重點,內容涵蓋 JDK