从Android相机聚焦和扫描条形码线

时间:2012-08-27 08:16:17

标签: android barcode-scanner

我正在使用以下内容扫描条形码行。

private Camera mCamera;
    private CameraPreview mPreview;
    public static final int MEDIA_TYPE_IMAGE = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newmain);

        Button captureButton = (Button) findViewById(R.id.button_capture);

//      Create an instance of Camera
        mCamera = getCameraInstance();

//      Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
        final PictureCallback mPicture = new PictureCallback() 
        {
            public void onPictureTaken(byte[] data, Camera camera)
            {
                Intent intent = new Intent("http://zxing.appspot.com/scan");
//              Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                startActivityForResult(intent, 0); 
            }
        };

            captureButton.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    Intent intent = new Intent("http://zxing.appspot.com/scan");
//                  Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0); 

                    // get an image from the camera   
//                    System.out.println("Photo Taking!");
//                    mCamera.takePicture(null, null, mPicture);
                }
            });
    }

然后我使用以下方法将意图传递给zxing sdk ..

public void onActivityResult(int requestCode, int resultCode, Intent intent) 
      {
          if (requestCode == 0)
          {
              TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
              TextView tvResult=(TextView)findViewById(R.id.tvResult);
              if (resultCode == RESULT_OK) 
              {
                  String contents = intent.getStringExtra("SCAN_RESULT");
                  String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                  tvStatus.setText(intent.getStringExtra(format));
                  tvResult.setText(intent.getStringExtra(contents));
                  Toast.makeText(getApplicationContext(), "Content:" + contents + " Format:" + format , Toast.LENGTH_LONG).show();
              }
              else if (resultCode == RESULT_CANCELED) 
              {
                  tvStatus.setText("Press a button to start a scan.");
                  tvResult.setText("Scan cancelled.");
              }
          }
      }

但我无法将相机对焦于条形码线上。 我很感激,如果我得到一些关于如何将相机聚焦在条形码线上的帮助,那么我可以将意图传递给zxing sdk .. 此外,评论一些Android sdk扫描条形码线将不胜感激.. Thanx提前..

1 个答案:

答案 0 :(得分:2)

你为什么要拍照并发送给zxing?您可以直接询问Zxing app打开相机并阅读条形码。请参阅我的回答here

相关问题