Search code examples
androidzbar

Camera is not working for scanning


implementation  of main class:    
    
public class Main Activity extends Activity implements ZBarScannerView.ResultHandler {
          
    private ZBarScannerView mscannerview ;
    @Override protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }

    public void onClick1(View view){
                
        mscannerview=new ZBarScannerView(this);                    
        setContentView(mscannerview);                    
        mscannerview.setResultHandler(this);
        mscannerview.startCamera();                

    }
        
    @Override public void onPause() {
                
        super.onPause();
        mscannerview.stopCamera();
        
     }
        
     @Override public void handleResult(Result result) {
                
         Log.w("handleResult", result.getContents());
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("Scan result");
         builder.setMessage(result.getContents());
         AlertDialog alertDialog = builder.create();
         alertDialog.show();
            
    }
}

Error :2-18 01:42:51.809 14705-14851/com.anewapplication W/CameraBase﹕ An error occurred while connecting to camera: 0

Its a simple implementation of Zbar library and my camera is not popping out for scanning ! Help me out ! Thanks in advance.

beginner in android.


Solution

  • You are not setting any Buttons in this code to be clicked. So if you are setting the OnClickListener directly from your xml layout then you should pass the view as parameter to the method that will handle its click event which i do not see on your method (you do not have any parameters).

    You can do like so :

    public void onClick1(View v){
        mscannerview=new ZBarScannerView(this);
        setContentView(mscannerview);
        mscannerview.setResultHandler(this);
        mscannerview.startCamera();
    }
    

    and in your layout :

    <Button android:onClick="onClick1"..... />