我的项目是在 Eclipse 上开发的。
首先下载ZXing lib
然后将此库作为库添加到您的应用中...
[右键单击您的项目->属性...]
然后写这段代码:
btnScan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG).show();
}
}
});
还有:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
textViewFormat.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
textViewData.setText(intent.getStringExtra("SCAN_RESULT"));
Uri imageURI = intent.getData();
Bitmap bitmap;
try{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
scannedBitmap.setImageBitmap(bitmap);
} catch(Exception e){
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT_FORMAT") + ":" + intent.getStringExtra("SCAN_RESULT"), 5000).show();
} else if (resultCode == RESULT_CANCELED) {
textViewFormat.setText("");
textViewData.setText("Cancelled By user");
}
}
完整项目下载我的 git repo
宣言:
您必须授予您的 android manifesto 文件权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".encode.EncodeActivity"
android:label="@string/app_name"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="com.google.zxing.client.android.ENCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- This allows us to handle the Share button in Contacts. -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
<!-- This allows us to handle sharing any plain text . -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>