簡單讀取QR Code訊息

1.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://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=".MainActivity" >

    <Button
        android:id="@+id/read_qr_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="18dp"
        android:text="Read QRCode" />

    <Button
        android:id="@+id/res_qr_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/read_qr_button"
        android:layout_below="@+id/read_qr_button"
        android:text="Resolve QRCode" />

    <ImageView
        android:id="@+id/image_qr"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/res_qr_button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

3.MainActivity.java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

Button readButton, resButton;
ImageView qrView;
Bitmap bitmap;
String tmp = "aa";
final String urlStr = "http://chart.apis.google.com/chart?cht=qr&chs=100x100&chl="
+ tmp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readButton = (Button) this.findViewById(R.id.read_qr_button);
resButton = (Button) this.findViewById(R.id.res_qr_button);
qrView = (ImageView) this.findViewById(R.id.image_qr);
readButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bimage = getBitmapFromURL(urlStr);
Message m = new Message();
m.obj = bimage;
handler.sendMessage(m);
}
}).start();
}
});

resButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
String result = decodeQRImage(bitmap);
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT)
.show();
}
});
}

Handler handler = new Handler() {
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
qrView.setImageBitmap(bitmap);
}
};

class MyQRCodeImage implements QRCodeImage {
Bitmap image;

public MyQRCodeImage(Bitmap image) {
this.image = image;
}

public int getWidth() {
return image.getWidth();
}

public int getHeight() {
return image.getHeight();
}

public int getPixel(int x, int y) {
return image.getPixel(x, y);
}
}

public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
this.bitmap = myBitmap;
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public String decodeQRImage(Bitmap image) {
String qrResult = "";
try {
QRCodeDecoder decoder = new QRCodeDecoder();
qrResult = new String(decoder.decode(new MyQRCodeImage(image)));
} catch (Exception e) {
e.printStackTrace();
}
return qrResult;
}

}

4.需下載qrcode.jar(點我連結下載點網頁)

沒有留言: