我有一个接收收据照片的应用程序,我想使用Zxing来读取这个位图,并提取二维码和条形码信息。这有可能吗?如果是,你能分享一下Android的代码吗?
发布于 2015-10-23 14:39:52
如果你不想坚持使用Zxing,你可以使用Google Play Service7.8版本提供的Barcode Scanning Apis。它具有读取各种条形码的能力。它可以将图像作为位图,也可以实时扫描条形码。假设你已经从图库中获得了一张图片,并将其转换为位图。请找到以下代码,以便使用此库发送要扫描的条形码图像。
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");
}在build.gradle文件中,在依赖项部分中包含以下内容:
compile 'com.google.android.gms:play-services:7.8.+'并添加以下清单权限:
<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" />此接口的详细使用方法请参考Github Sample,遵循Code Lab,Documentation。
发布于 2015-10-16 22:55:40
它非常简单,您需要调用startActivityforResult,类似于调用另一个activity。
在调用扫描时的操作中,您需要调用以下内容:
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.
}
}内容将是您需要的条形码编号。
发布于 2015-10-21 13:49:30
您可以使用各种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;
}https://stackoverflow.com/questions/33172719
复制相似问题