我想在我的应用程序中实现一个 QR 码/条形码阅读器。我想知道什么是最轻量级的解决方案(不考虑 zxing 的意图集成器)。
问问题
10764 次
3 回答
10
我使用 zxing 构建到我的应用程序中。您将需要一些编码。首先在你的构建路径中包含 core.jar,它位于 core/core.jar,然后转到他们的客户端,它位于 android/..../com.google.zxing,并获取他们的代码(这是不推荐的开发人员,因为您的复制和粘贴。)最后,添加以下代码:
package com.wtsang02.activities;
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.HybridBinarizer;
public class QRDecoder extends Activity implements OnClickListener {
private String text;
private Button webbutton;
private Bitmap bmp;
private ImageView ivPicture;
private TextView textv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mysales);
webbutton = (Button)findViewById(R.id.webbutton);
ivPicture = (ImageView) findViewById(R.id.ivPicture);
textv= (TextView) findViewById(R.id.mytext);
webbutton.setOnClickListener(this);
}
private void decode() {
if (bmp == null) {
Log.i("tag", "wtf");
}
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),
bmp.getHeight());
LuminanceSource source = new com.google.zxing.RGBLuminanceSource(
bmp.getWidth(), bmp.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap);
text = result.getText();
byte[] rawBytes = result.getRawBytes();
BarcodeFormat format = result.getBarcodeFormat();
ResultPoint[] points = result.getResultPoints();
textv.setText(text);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
Log.i("done", "done");
if(text!=null)
Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
else{
Toast.makeText(getBaseContext(), "QQ", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
ivPicture.setImageBitmap(bmp);
decode();
}
}
}
此代码将使用您手机的默认摄像头,如果您需要使用他们的客户端,则需要启动他们的CaptureActivity
,您的布局应包括TextView
显示结果、ImageView
显示您捕获的图像以及Button
启动摄像头。. 这是基于 2.1zxing 的。
于 2012-12-10T15:31:55.237 回答
0
嘿,从此链接下载 Biggu Barcode Scanner 的示例,提取演示项目并将其导入 eclipse。zip 文件有演示示例,您可以根据需要使用并集成到您的应用程序中
于 2012-12-10T15:32:09.340 回答