在開發之前要先下載zxing包對其簡化,新建一個android工程,把zxing包中的android和android-code中的文件複製到相應的文件夾裏面
其中的libs文件夾裏需要zxing包中的core文件編譯成.jar放在libs文件中,如果出現swich中的r.id.xx報錯吧swich改成if語句,如:
if (message.what == R.id.decode) {
decode((byte[]) message.obj, message.arg1, message.arg2);
} else if (message.what == R.id.quit) {
running = false;
Looper.myLooper().quit();
}
然後右鍵項目選擇屬性勾選 is library
然後將新建我們的項目,同樣右鍵選擇屬性add一下剛剛創建的library然後ok
接下來首先要在項目中添加相應的權限
<uses-permission android:name="android.permission.VIBRATE" /> <!-- 震動權限 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" /> <!-- 使用照相機權限 -->
<uses-feature android:name="android.hardware.camera.autofocus" /> <!-- 自動聚焦權限 -->
然後再activity中添加相應的佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn_scan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="掃描二維碼" />
<TextView
android:id="@+id/tv_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="word" />
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請輸入要編碼的內容">
</EditText>
<Button
android:id="@+id/gen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生成二維碼" />
<Button
android:id="@+id/genone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生成一維碼" />
<ImageView
android:id="@+id/imsge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/tv_textone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
生成二維碼的相關代碼,傳入要編碼的參數,其中的大小也可寫成參數傳入,這裏就不信寫
/*
* 二維碼
*/
public Bitmap CreateTwoDCode(String content) throws WriterException {
// 生成二維矩陣,編碼時指定大小,不要生成了圖片以後再進行縮放,這樣會模糊導致識別失敗
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, 300, 300);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二維矩陣轉爲一維像素數組,也就是一直橫着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
生成一維碼的代碼,參數同上
/*
* 一維碼
*/
public Bitmap CreateOneDCode(String content) throws WriterException {
// 生成一維條碼,編碼時指定大小,不要生成了圖片以後再進行縮放,這樣會模糊導致識別失敗
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.CODE_128, 500, 200);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
所有代碼(記得註冊頁面)
package com.example.myscan;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.common.BitMatrix;
public class MainActivity extends Activity implements OnClickListener {
private Button btn, genone, gen;
private TextView tv, textone;
private ImageView img;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gen = (Button) findViewById(R.id.gen);
img = (ImageView) findViewById(R.id.imsge);
btn = (Button) findViewById(R.id.btn_scan);
tv = (TextView) findViewById(R.id.tv_scan);
et = (EditText) findViewById(R.id.input);
genone = (Button) findViewById(R.id.genone);
textone = (TextView) findViewById(R.id.tv_textone);
btn.setOnClickListener(this);
gen.setOnClickListener(this);
genone.setOnClickListener(this);
initView();
}
private void initView() {
// TODO Auto-generated method stub
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String str = data.getExtras().getString("result");
tv.setText(str);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textone.setText("");
switch (v.getId()) {
case R.id.btn_scan:
Log.i("tag", "--->1");
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
Log.i("tag", "--->3");
startActivityForResult(intent, 0);
Log.i("tag", "--->4");
break;
case R.id.gen:
String str = et.getText().toString();
if (str.equals("")) {
Toast.makeText(MainActivity.this, "請輸入信息", Toast.LENGTH_SHORT)
.show();
} else {
try {
Bitmap bit = CreateTwoDCode(str);
img.setImageBitmap(bit);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case R.id.genone:
String strr = et.getText().toString();
if (strr.equals("")) {
Toast.makeText(MainActivity.this, "請輸入信息", Toast.LENGTH_SHORT)
.show();
} else {
try {
Bitmap bit = CreateOneDCode(strr);
img.setImageBitmap(bit);
textone.setText(strr);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
break;
}
}
/*
* 二維碼
*/
public Bitmap CreateTwoDCode(String content) throws WriterException {
// 生成二維矩陣,編碼時指定大小,不要生成了圖片以後再進行縮放,這樣會模糊導致識別失敗
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, 300, 300);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二維矩陣轉爲一維像素數組,也就是一直橫着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/*
* 一維碼
*/
public Bitmap CreateOneDCode(String content) throws WriterException {
// 生成一維條碼,編碼時指定大小,不要生成了圖片以後再進行縮放,這樣會模糊導致識別失敗
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.CODE_128, 500, 200);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
其中實現了一維碼和二維碼的生成和掃描,其中的條形碼還沒有加只能輸入數字的限制,看效果圖(沒有掃描的圖)