Reputation: 109
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.
Upvotes: 1
Views: 411
Reputation: 8723
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();
Upvotes: 1