简体   繁体   中英

How to get a preview of camera

I used that example to get preview from camera:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TextureView;
import android.widget.FrameLayout;
import android.view.TextureView;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;


public class MainActivity extends Activity implements
TextureView.SurfaceTextureListener, Camera.PreviewCallback {

    static {
        System.loadLibrary("JNIProcessor");
    }

    private final String TAG="LiveFeature";

    private Camera mCamera;
    private byte[] mVideoSource;

    private TextureView mTextureView;

    private String[] ResolutionList;
    private Menu AppMenu;

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


        mTextureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture pSurface,
            int pWidth, int pHeight) {
        // Ignored
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture pSurface) {
        // Ignored
    }

    @Override
    public void onPreviewFrame(byte[] pData, Camera pCamera) {
    }
}

It seems the Camera.PreviewCallback is deprecated and I should use android.hardware.camera2. The problem is I don't find preview callback function to get the raw data bytes. I just want to grabe frame from camera without render it into a surface and to put it into a JNI function.

These steps to get a preview:

  • Add camera permission in manifest file (Manifest.permission.CAMERA)

  • Get camera instance with this method:

     public static Camera getCameraInstance() { Camera c = null; try { c = Camera.open(); // attempt to get a Camera instance } catch (Exception e) { Log.e(TAG, e.getMessage()); // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable } 
  • Create a class that extends SurfaceView and let the class implement SurfaceHolder.Callback and pass your Camera instance in its constructor. Get the holder in the constructor with getHolder()

  • In method "surfaceCreated", set preview display and start preview

    public void surfaceCreated(SurfaceHolder holder) { try { mCameraSource.start(holder); mCamera.setPreviewDisplay(mHolder); setWillNotDraw(false); } catch (Exception e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } }

  • Stop preview and release camera when you don't need it anymore

    mCamera.stopPreview(); mCamera.release();

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