前言:
有別於掃描的用法。這次是在App內生成QRCode。
情境可以是給她一個網址參數,或任何字串,然後產生QRcode提供掃描。
像是購物網站,當你買了物品,你想追蹤商品情況時,購物網站可以在訂單查詢內提供QRCode供你掃描查看商品跑去哪了。
可以看看這第三方 https://github.com/zxing/zxing
實作:
等等要操作到的地方STEP 1 :先匯入專案
兩種匯入方式 兩種選擇其中一種就好,別傻傻的兩個都匯入
第一種
第一種是直接compile
compile 'com.google.zxing:core:3.2.1'
第二種
先把core-3.2.1.jar下載下來。
然後就開始匯入專案了。
匯入專案的地方可以參考這一篇:
http://nikeru8.blogspot.tw/2017/03/androidyoutube-api-youtube-api.html
我在這邊使用另外一種方式,不過大同小異。
下載下來後,把core-3.2.1.jar放到你自己專案的資料夾libs內。(如果沒有此資料夾請自己創)
![]() |
| 如果圖片太小可點擊放大 |
點選左上角選擇「Project」>> 找到「libs」>> 找到「core-3.2.1.jar」 >> 點擊右鍵 >> 選擇「Add As Library...」
跳出視窗選擇「app」 >> ok
專案匯入完成囉!
STEP 2:來畫我們的xml
![]() |
| activity_main.xml |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:gravity="center"
android:onClick="onbuttonclick"
android:text="產生QRCode" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp" />
</RelativeLayout>
設置一個Button,等點擊之後生成QRCode
STEP 3:寫Code in JAVA
MainActivity.javapublic class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onbuttonclick(View view) {
String QRCodeContent = "http://nikeru8.blogspot.tw/2017/04/third-partyxzingcore-qrcodejar.html";
int QRCodeWidth = 200;
int QRCodeHeight = 200;
//QRCode內容編碼
Map hints = new EnumMap(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter writer = new MultiFormatWriter();
try {
//ErrorCorrectionLevel容錯率分四級:L(7%) M(15%) Q(25%) H(30%)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//建立QRCode的資料矩陣
BitMatrix result = writer.encode(QRCodeContent, BarcodeFormat.QR_CODE, QRCodeWidth, QRCodeHeight, hints);
//建立矩陣圖
Bitmap bitmap = Bitmap.createBitmap(QRCodeWidth, QRCodeHeight, Bitmap.Config.ARGB_4444);
for (int y = 0; y < QRCodeHeight; y++) {
for (int x = 0; x < QRCodeWidth; x++) {
bitmap.setPixel(x, y, result.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
//設定給xml
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
}
完成囉!
QRCodeContent內就是QRCode產生出來的字串。
eidt(2017/06/12)
我發現如果QRCode你要放到很大很大,直接調整QRCodeWidth和Height會因為產生bitmap而有卡頓狀況。
此時可以用一個方法改善
bitmap = Bitmap.createScaledBitmap(bitmap,
bitmap.getWidth() * 4,
bitmap.getHeight() * 4, false);
eidt(2017/11/27)
兩件事
一、控制QRCode旁邊白邊的大小
hints.put(EncodeHintType.MARGIN, 0);後面的0 這個int參數,設得越大白邊就越寬
二、QRCode圖片太大有時候會造成app卡頓
使用AsyncTask http://nikeru8.blogspot.tw/2017/11/androidasynctask_26.html
文獻:
http://gnehcic.azurewebsites.net/android%E5%BB%BA%E7%AB%8Bqr-code/
https://github.com/zxing/zxing
Demo:
https://drive.google.com/open?id=0Byk75IYx-dKXT0dERGdqRVNYSEk





沒有留言:
張貼留言