我有一个接收收据照片的应用程序,我想使用Zxing来读取这个位图并提取QR码和条形码信息。这可能吗?如果有,能否分享一下Android的代码?
如果你不想坚持Zxing,你可以去从Google Play Service 7.8版本提供的条形码扫描api。它具有读取各种条形码的能力。它既可以将图像作为位图,也可以实时扫描条形码。假设您从图库中获得了一张图像并将其转换为位图。请找到下面的代码发送条形码图像要扫描使用此库。
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
.build();
if(barcode.isOperational()){
SparseArray<Barcode> sparseArray = barcodeDetector.detect(frame);
if(sparseArray != null && sparseArray.size() > 0){
for (int i = 0; i < sparseArray.size(); i++){
Log.d(LOG_TAG, "Value: " + sparseArray.valueAt(i).rawValue + "----" + sparseArray.valueAt(i).displayValue);
Toast.makeText(LOG_TAG, sparseArray.valueAt(i).rawValue, Toast.LENGTH_SHORT).show();
}
}else {
Log.e(LOG_TAG,"SparseArray null or empty");
}
}else{
Log.e(LOG_TAG, "Detector dependencies are not yet downloaded");
}
在你的构建。在Gradle文件中,在dependencies部分包含以下内容:
compile 'com.google.android.gms:play-services:7.8.+'
,并添加以下Manifest权限:
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Meta data for google play services: -->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<!-- Meta data for first time install/run time dependencies to be downloaded for getting barcode detector operational -->
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode" />
有关此api的详细使用,请参阅Github Sample,参见Code Lab, Documentation.
您可以使用ZXing的各种分支。
例如:https://zxingnet.codeplex.com/
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = (Bitmap)Bitmap.LoadFrom("C:\sample-barcode-image.png");
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
// do something with the result
if (result != null)
{
txtDecoderType.Text = result.BarcodeFormat.ToString();
txtDecoderContent.Text = result.Text;
}
这很简单,你需要调用startActivityforResult,类似于调用另一个活动。
在您的操作中,当扫描被调用时,您需要以下调用:
public InvokeScan()
{
mAppPAckage="com.google.zxing.client.android.SCAN"
Intent intentScan = new Intent(mAppPackage);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// set the desired barcode types
intentScan.putExtra("SCAN_FORMATS", stringDesiredBarcodeFormats);
final PackageManager packageManager = activity.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intentScan,
PackageManager.MATCH_DEFAULT_ONLY);
activity.startActivityForResult(intentScan,REQUEST_CODE);
onActivityResult,您需要捕获结果
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
String desiredBarCodeFormat = BarCodeActivity.sDesiredBarcodeFormatValue;*/
String contents = intent.getStringExtra(activityBundleName);
String formatName = intent.getStringExtra(desiredBarCodeFormat);
//do whatever you want from contents.
}
}
Contents将是您需要的条形码编号。