1

我正在尝试访问 android 手机的前置摄像头并尝试在没有任何预览的情况下不时捕获图像。当我不使用线程时,我的代码运行良好。我的代码在后台服务中运行良好,但是当我应用线程时,我得到了一个异常。异常告诉您无法访问相机信息。实现的最佳方法是什么??????

这是我的代码,效果很好。

我想为我的这个应用程序应用线程。我该怎么做????

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;


public class MainActivity extends Activity {

     Button front;
     private Camera mCamera=null;
     private Parameters parameters;
     private SurfaceHolder sHolder;  

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

        addListnerOnButton();
    }



public void addListnerOnButton(){

        front=(Button)findViewById(R.id.btn);

        front.setOnClickListener(new  OnClickListener(){

            @Override
            public void onClick(View arg0) {


                    for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
                        CameraInfo camInfo = new CameraInfo();
                        Camera.getCameraInfo(camNo, camInfo);

                        if (camInfo.facing==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
                            //mCamera = Camera.open(camNo);


                            //toastCam(camNo);

                        takePic(camNo);


                        }



                    }




            }




        });





    }



/*public void toastCam(int id){

    Toast.makeText(this,String.valueOf(id), Toast.LENGTH_LONG).show();
}
*/

public void takePic(int no){


     mCamera = Camera.open(no);

     SurfaceView sv = new SurfaceView(getApplicationContext());


     try {
                mCamera.setPreviewDisplay(sv.getHolder());
                parameters = mCamera.getParameters();

                 //set camera parameters
               mCamera.setParameters(parameters);
               mCamera.startPreview();
               mCamera.takePicture(null, null, mCall);


          } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
          }


     //Get a surface
       sHolder = sv.getHolder();
      //tells Android that this surface will have its data constantly replaced
       sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);







}




Camera.PictureCallback mCall = new Camera.PictureCallback()
{

   public void onPictureTaken(byte[] data, Camera camera)
   {
         //decode the data obtained by the camera into a Bitmap

         FileOutputStream outStream = null;
              try{
                  outStream = new FileOutputStream("/sdcard/Image.jpeg");
                  outStream.write(data);
                  outStream.close();
                  mCamera.release();

              } catch (FileNotFoundException e){

              } catch (IOException e){

              }

   }
};



}
4

0 回答 0