简体   繁体   English

Android Camera:在单击按钮时调用预览回调

[英]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. 我想在单击按钮时从相机获取PreviewFrames。但是我不知道该如何做,因为是Android新手。 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 如果正确,则可以在Camera类中使用方法takePicture();如果不正确,则可以在onPreviewFrame方法中使用一个标志,该标志可以由onClickListener更改为true,并且在完成操作后应更改为false

OnPreviewFrame() is a method within the PreviewCallback interface. OnPreviewFrame()是PreviewCallback接口中的方法。 So I think that you need to create a class implementing that interface, and to call camera.setPreviewCallback() with that object. 因此,我认为您需要创建一个实现该接口的类,并使用该对象调用camera.setPreviewCallback()。 Careful cause OnPreviewFrame is called every time a frame is available aka it will be called several times as the preview is runing. 小心,原因是每当有可用框架时都会调用OnPreviewFrame,也就是在运行预览时会多次调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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