如何获取条码扫描仪 android 的 activity 结果?
How to get the activity result for barcode scanner android?
如何使用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
我解决了。我就是这样做的。
我创建了第二个 activity CustomScannerActivity。我在其中做扫描部分。
public class CustomScannerActivity extends Activity {
private CaptureManager capture;
private CompoundBarcodeView barcodeScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner);
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
}
然后从第一个开始activity。我从这个调用了 CustomScannerActivity。所以现在你会得到结果。希望能帮助到你。确保您也在清单中声明了活动,这样它才能工作。
public class ScannerActivity extends Activity {
ResultHandler resultHandler;
Parameters parameters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CustomScannerActivity.class);
integrator.initiateScan();
}
@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);
}
// else continue with any other code you need in the method
this.finish();
}
}
如何使用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
我解决了。我就是这样做的。
我创建了第二个 activity CustomScannerActivity。我在其中做扫描部分。
public class CustomScannerActivity extends Activity {
private CaptureManager capture;
private CompoundBarcodeView barcodeScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner);
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
}
然后从第一个开始activity。我从这个调用了 CustomScannerActivity。所以现在你会得到结果。希望能帮助到你。确保您也在清单中声明了活动,这样它才能工作。
public class ScannerActivity extends Activity {
ResultHandler resultHandler;
Parameters parameters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CustomScannerActivity.class);
integrator.initiateScan();
}
@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);
}
// else continue with any other code you need in the method
this.finish();
}
}