我想在我的应用程序中实现QR Code / Barcode阅读器。我想知道什么是最轻量级的解决方案(无视zxing的意图集成商)。
答案 0 :(得分: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
以显示结果,{{1} }显示您捕获的图像,ImageView
启动相机。 。这是基于2.1zxing。
答案 1 :(得分:2)
答案 2 :(得分:0)
从这个link下载Biggu条码扫描器的样本,提取演示项目并将其导入eclipse。该zip文件包含演示示例,您可以根据需要使用并集成到您的应用程序中