简体   繁体   中英

Android Camera: call Preview callback on Button click

Am displaying a camera inside a framelayout. i would like to get the PreviewFrames from the camera on a button click.But i dont know how to do that as am an android newbie. Any help would be appreciated.thanks

My code is below:

    public class MainActivity extends Activity {
    private Camera mCamera;
    private CameraPreview mPreview;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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);
    mCamera.setDisplayOrientation(90);
    preview.addView(mPreview);


      findViewById(R.id.capture).setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {


           }
        });         

    }

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
   }



   public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
  }






public void onPreviewFrame(byte[] data, Camera camera) {
    try {
        Camera.Parameters parameters = camera.getParameters();
        Size size = parameters.getPreviewSize();
        YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
                size.width, size.height, null);
        File file = new File(Environment.getExternalStorageDirectory()
                .getPath() + "/out.jpg");
        FileOutputStream filecon = new FileOutputStream(file);
        image.compressToJpeg(
                new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
                filecon);
     } catch (FileNotFoundException e) {
        Toast toast = Toast
                .makeText(getBaseContext(), e.getMessage(), 1000);
        toast.show();
    }
    }



    }

first of all, sorry for my poor eng ! does the word "getting camera preview frames" mean that you wanna take picture ? if right, you can use the method takePicture() in Camera class if not, you can use a flag in onPreviewFrame method that can be changed to true by your onClickListener and should be changed to false after doing stuffs

OnPreviewFrame() is a method within the PreviewCallback interface. So I think that you need to create a class implementing that interface, and to call camera.setPreviewCallback() with that object. Careful cause OnPreviewFrame is called every time a frame is available aka it will be called several times as the preview is runing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM