如何在Android中使用Zxing读取代码39? [英] How to read code 39 using zxing in android?

查看:93
本文介绍了如何在Android中使用Zxing读取代码39?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Android应用程序中使用zxing读取QR_CODE和条形码.我的应用程序无法使用zxing读取CODE_39.我在CaptureActivity OnResume方法中使用以下代码:

I am using zxing in my android application to read QR_CODE and Barcodes. My application is unable to read the CODE_39 using zxing. I am using the following code in CaptureActivity OnResume Method:

Intent intent = getIntent();
        String action = intent == null ? null : intent.getAction();
        String dataString = intent == null ? null : intent.getDataString();
        if (intent != null && action != null) {
            if (action.equals(Intents.Scan.ACTION)) {
                 //Scan the formats the intent requested, and return the
                 //result
                 //to the calling activity.
                source = Source.NATIVE_APP_INTENT;
                decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);

            } else if (dataString != null
                    && dataString.contains(PRODUCT_SEARCH_URL_PREFIX)
                    && dataString.contains(PRODUCT_SEARCH_URL_SUFFIX)) {
                // Scan only products and send the result to mobile Product
                // Search.
                source = Source.PRODUCT_SEARCH_LINK;
                sourceUrl = dataString;
                decodeFormats = DecodeFormatManager.PRODUCT_FORMATS;
            } else if (dataString != null
                    && dataString.startsWith(ZXING_URL)) {
                // Scan formats requested in query string (all formats if
                // none
                // specified).
                // If a return URL is specified, send the results there.
                // Otherwise, handle it ourselves.
                source = Source.ZXING_LINK;
                sourceUrl = dataString;
                Uri inputUri = Uri.parse(sourceUrl);
                returnUrlTemplate = inputUri
                        .getQueryParameter(RETURN_URL_PARAM);
                decodeFormats = DecodeFormatManager
                        .parseDecodeFormats(inputUri);
            } else {
                // Scan all formats and handle the results ourselves
                // (launched
                // from Home).
                source = Source.NONE;
                decodeFormats = null;
                }

            characterSet = intent
                    .getStringExtra(Intents.Scan.CHARACTER_SET);

请帮助我解决此问题.预先感谢.

Please Help me to solve this issuse. Thanks in advance.

推荐答案

如果您使用的是Android Studio,请添加这些依赖项-

If you are using Android Studio then Add those dependancies-

compile 'me.dm7.barcodescanner:zxing:1.8.3'
compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'

Zxing扫描时自动获取代码类型

Zxing Automatically takes code type while scanning

integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES) 

此处默认情况下考虑所有类型的代码

Here which is consider all types of codes by default

如果您想要特定的QR,则只需

If you want specific QR then just

integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);

使用以下代码-

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class YourActivity extends Activity {


//Barcode Scanning
private ZXingScannerView mScannerView;


// This is your click listener
public void checkBarcode(View v) {
    try {
        IntentIntegrator integrator = new IntentIntegrator(GateEntryActivity.this);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
        integrator.setPrompt("Scan a barcode");
        integrator.setCameraId(0);  // Use a specific camera of the device
        integrator.setBeepEnabled(false);
        integrator.initiateScan();
        //start the scanning activity from the com.google.zxing.client.android.SCAN intent
        // Programmatically initialize the scanner view
        // setContentView(mScannerView);
    } catch (ActivityNotFoundException anfe) {
        //on catch, show the download dialog
        showDialog(GateEntryActivity.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
}


//alert dialog for downloadDialog
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
    downloadDialog.setTitle(title);
    downloadDialog.setMessage(message);
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                act.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {

            }
        }
    });
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return downloadDialog.show();
}

//on ActivityResult method
@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("MainActivity", "Cancelled scan");
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Log.d("MainActivity", "Scanned");
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();                
        }
    } else {
        Log.d("MainActivity", "Weird");
        // This is important, otherwise the result will not be passed to the fragment
        super.onActivityResult(requestCode, resultCode, data);
    }
}

}

这篇关于如何在Android中使用Zxing读取代码39?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆