How to read barcodes with the camera on Android?

前端 未结 9 1613
广开言路
广开言路 2020-12-07 19:24

I want my application to recognize barcodes taken by camera. Is it possible using Android SDK?

Something like this: Barcode Scanner

9条回答
  •  情书的邮戳
    2020-12-07 19:30

    I had an issue with the parseActivityForResult arguments. I got this to work:

    package JMA.BarCodeScanner;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class JMABarcodeScannerActivity extends Activity {
    
        Button captureButton;
        TextView tvContents;
        TextView tvFormat;
        Activity activity;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            activity = this;
            captureButton = (Button)findViewById(R.id.capture);
            captureButton.setOnClickListener(listener);
            tvContents = (TextView)findViewById(R.id.tvContents);
            tvFormat = (TextView)findViewById(R.id.tvFormat);
        }
    
        public void onActivityResult(int requestCode, int resultCode, Intent intent) 
        {  
            switch (requestCode) 
            {
                case IntentIntegrator.REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) 
                {
                    IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    
                    if (intentResult != null) 
                    {
                       String contents = intentResult.getContents();
                       String format = intentResult.getFormatName();
                       tvContents.setText(contents.toString());
                       tvFormat.setText(format.toString());
    
                       //this.elemQuery.setText(contents);
                       //this.resume = false;
                       Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
                    } else {
                        Log.e("SEARCH_EAN", "IntentResult je NULL!");
                    }
                } 
                else if (resultCode == Activity.RESULT_CANCELED) {
                    Log.e("SEARCH_EAN", "CANCEL");
                }
            }
        }
    
        private View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 IntentIntegrator integrator = new IntentIntegrator(activity);
                 integrator.initiateScan();
            }
        };
    }
    

    Latyout for Activity:

    
    
        

提交回复
热议问题