如何使用 zxing 库获取自定义扫描仪的扫描条码结果? Activity 结果不起作用。扫描部分工作正常并得到结果。但我没有在 Activity 结果中获取任何数据。
public class ScannerActivity extends Activity implements ZXingScannerView.ResultHandler{
ResultHandler resultHandler;
Parameters parameters;
private CaptureManager capture;
private CompoundBarcodeView barcodeScannerView;
private Button switchFlashlightButton;
private ZXingScannerView mScannerView;
BarcodeView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner);
Bundle extras = this.getIntent().getExtras();
resultHandler = (ResultHandler) extras.getSerializable("RESULT_HANDLER");
parameters = (Parameters) extras.getSerializable("PARAMETERS");
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
this.getIntent().putExtra("Result_handle",resultHandler);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.d("onActivityResult", "onActivityResult: .");
if (resultCode == Activity.RESULT_OK) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
String re = scanResult.getContents();
String message = re;
Log.d("onActivityResult", "onActivityResult: ."+ re);
Result handlerResult = new Result(Result.STATUS_SUCCESS, "qrcode", message);
resultHandler.onHandleResult(handlerResult);
this.finish();
}
// else continue with any other code you need in the method
}
@Override
protected void onResume() {
Log.d("onResume", "onResume: .");
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
Log.d("onPause", "onPause: .");
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d("onSaveInstanceState", "onSaveInstanceState: .");
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
}
最佳答案
您已授予声明文件权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_EXTERN
AL_STORAGE" />
然后在manifesto应用程序标签中添加以下代码:
<activity
android:name=".encode.EncodeActivity"
android:label="@string/app_name"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="com.google.zxing.client.android.ENCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- This allows us to handle the Share button in Contacts. -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
<!-- This allows us to handle sharing any plain text . -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
然后你的 onActivityResult() 方法将如下所示:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
textViewFormat.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
textViewData.setText(intent.getStringExtra("SCAN_RESULT"));
Uri imageURI = intent.getData();
Bitmap bitmap;
try{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
scannedBitmap.setImageBitmap(bitmap);
} catch(Exception e){
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT_FORMAT") + ":" + intent.getStringExtra("SCAN_RESULT"), 5000).show();
} else if (resultCode == RESULT_CANCELED) {
textViewFormat.setText("");
textViewData.setText("Cancelled By user");
}
}
}
/**
* This method used for converting BitMatrix to BitMap
* @param matrix
* @return bitmap
*/
public static Bitmap toBitmap(BitMatrix bitMatrix){
int height = bitMatrix.getHeight();
int width = bitMatrix.getWidth();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++){
bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
}
查看我的git source code
关于java - 如何获取条码扫描仪android的 Activity 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35041607/