扫描后ZXing双重图像/叠加

时间:2016-03-18 16:49:25

标签: android barcode zxing

我在我的Android应用中使用与jar文件完全集成的ZXing库,并出现以下问题:

问题:

扫描条形码后,扫描图像保持在实时相机进纸的顶部,透明度约为50%,持续约1-2秒。

问题:

有没有办法让扫描图像显示为0%透明度,而不是奇怪的叠加?或者,更好的是,它可以显示自定义片段吗?

谢谢。

代码:[没有相关部分]

public static void initiateScan(Fragment fragment) {
    IntentIntegrator ii = new IntentIntegrator(fragment);
    DisplayMetrics dm = fragment.getResources().getDisplayMetrics();
    ii.addExtra("SCAN_WIDTH", dm.heightPixels);
    ii.addExtra("SCAN_HEIGHT", dm.widthPixels / 4);
    ii.addExtra("SCAN_MODE", "ONE_D_MODE");

    List<String> c = new ArrayList<String>();
    c.add("CODE_39");

    ii.initiateScan(c, -1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==IntentIntegrator.REQUEST_CODE) { // scan from ZXing
        String raw_vin=null;
        String vin = null;
        boolean success=false;

        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, 
                                resultCode, data);
        if(result!=null)
        {
            String content = result.getContents();
            if(content!=null)
            {
                raw_vin=content;
                vin=raw_vin;
                success=true;
            }
        }
    }
}

示例:

Example of double image

3 个答案:

答案 0 :(得分:1)

以下是我在 build.gradle 文件中的依赖项:

compile 'com.google.zxing:core:3.2.1'
compile 'com.journeyapps:zxing-android-embedded:3.0.3@aar'

initiateScan 方法中尝试此操作:

public static void initiateScan(Fragment fragment) {
    IntentIntegrator ii = IntentIntegrator.forSupportFragment(fragment);
    DisplayMetrics dm = fragment.getResources().getDisplayMetrics();
    ii.addExtra("SCAN_WIDTH", dm.heightPixels);
    ii.addExtra("SCAN_HEIGHT", dm.widthPixels / 4);
    ii.addExtra("SCAN_MODE", "ONE_D_MODE");
    ii.initiateScan(Collections.singletonList("CODE_39"));
}

让我知道这对你有用。

答案 1 :(得分:0)

我无法解释为什么会出现这个问题,但我正在使用ZXing作为我的条形码扫描仪应用程序而不是50%透明。以下是我的解决方案:

<强>布局:

<com.journeyapps.barcodescanner.CompoundBarcodeView
    android:id="@+id/barcode_scanner"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="2dp">

</com.journeyapps.barcodescanner.CompoundBarcodeView>

片段:

private CompoundBarcodeView barcodeView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        View v = inflater.inflate(R.layout.webservice_layout, container, false);

        barcodeView = (CompoundBarcodeView) v.findViewById(R.id.barcode_scanner);

        barcodeView.decodeContinuous(callback);
......
}

回调功能:

private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {

            if (SOAP_ACTION.equals("") || SOAP_METHOD.equals("") || soapProperty.equals("")) {
                barcodeView.pause();
            } else {
                if (result.getText() != null) {
                    barcodeView.setStatusText(result.getText());

                    barcodeView.pause();

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            barcodeView.resume();
                        }
                    }, 1000);
                }
            }
        }
};

希望这有帮助!

答案 2 :(得分:0)

我的方法如下:

//start the scanning
public void startQRCodeScanning() {
    IntentIntegrator integrator = IntentIntegrator.forFragment(this);
    integrator.setCaptureActivity(ScanActivity);
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
    integrator.setBeepEnabled(false);
    integrator.setOrientationLocked(true);
    integrator.initiateScan();
}

//onActivityResult:
 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            Log.d(TAG, "Cancelled scan");
            //we want to finish the current activity
            //as it does not make sense to do anything else
            finishActivity();
        } else {
            Log.d(TAG, "Scanned " + result.getContents());
            setConnectionString(result.getContents());
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

//next I have the QRCodeScanningActivity

    public class QRCodeScannerActivity extends Activity {

    private CaptureManager capture;
    private CompoundBarcodeView barcodeScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_scanner);

        if (getResources().getBoolean(R.bool.portrait_only)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
        }

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 }

 //Next you need to define the layouts and everything.
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">

    <com.journeyapps.barcodescanner.CompoundBarcodeView
            android:id="@+id/zxing_barcode_scanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:zxing_scanner_layout="@layout/barcode_scanner_layout"/>

    </FrameLayout>
</RelativeLayout>