简体   繁体   中英

How to get the activity result for barcode scanner android?

How to get the scanned barcode result for a custom scanner using zxing library ? 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:

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

I solved it. This is how I did it.

I created a second activity 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. 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();

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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