简体   繁体   English

如何获取条形码扫描仪android的活动结果?

[英]How to get the activity result for barcode scanner android?

How to get the scanned barcode result for a custom scanner using zxing library ? 如何使用zxing库获取自定义扫描仪的扫描条形码结果? On activity result is not working. 开活动结果不起作用。 The scanning part is working fine and its getting the result. 扫描部分工作正常,并获得了结果。 But I am not getting any data in the activity result. 但是我没有在活动结果中得到任何数据。

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);
}

} }

[ FULL SOURCE CODE EXAMPLE ] [全源代码示例]

You have give permission into manifesto file: 您已将权限授予清单文件:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_EXTERN
AL_STORAGE" />

And then add this following code in manifesto application tag: 然后在清单应用程序标记中添加以下代码:

<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>

And then your onActivityResult() method will be look like this: 然后,您的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;
}

See my git source code 看到我的git源代码

I solved it. 我解决了 This is how I did it. 这就是我做的。

I created a second activity CustomScannerActivity. 我创建了第二个活动CustomScannerActivity。 In which I do the scanning part. 我在其中进行扫描。

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);
}

}

Then from the first activity. 然后从第一个活动开始。 I called the CustomScannerActivity from this one. 我从这一过程中调用了CustomScannerActivity。 So that now you will get the result. 这样,您就可以得到结果。 Hope it helps. 希望能帮助到你。 Make sure that you declare the activities in the manifest also so that it will work. 确保同时在清单中声明活动,以便活动起作用。

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();

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM