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

时间:2016-01-27 15:36:30

标签: java android android-activity barcode zxing

如何使用zxing库获取自定义扫描仪的扫描条形码结果?在活动结果不起作用。扫描部分工作正常,它得到的结果。但是我没有在活动结果中获得任何数据。

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

}

2 个答案:

答案 0 :(得分:1)

[ FULL SOURCE CODE EXAMPLE ]

您已向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>

然后在manifesto应用程序标记中添加以下代码:

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

然后你的onActivityResult()方法将如下所示:

.load()

查看我的git source code

答案 1 :(得分:0)

我解决了。这就是我做到的。

我创建了第二个活动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);
}

}

然后从第一个活动开始。我从这个调用了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();

}

}