i'm trying the camera preview
This is my code and it doesn't throw any error, but the screen is still black. Any ideas?
this.setContentView(R.layout.camerapreview);
SurfaceView cameraSurface = (SurfaceView)findViewById(R.id.cpPreview);
SurfaceHolder holder = cameraSurface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.camera = Camera.open();
this.camera.setPreviewDisplay(holder);
this.camera.startPreview();
camerapreview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SurfaceView
android:id="@+id/cpPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</SurfaceView>
</LinearLayout>
PreviewView is a subclass of FrameLayout . To display the camera feed, it uses either a SurfaceView or TextureView , provides a preview surface to the camera when it's ready, tries to keep it valid as long as the camera is using it, and when released prematurely, provides a new surface if the camera is still in use.
To force portrait orientation: set android:screenOrientation="portrait" in your AndroidManifest. xml and call camera. setDisplayOrientation(90); before calling camera.
You are calling the last three lines too early. You have to wait for the surface to be prepared before calling setPreviewDisplay()
and you have to wait for the surface to be sized (surfaceChanged()
) before calling startPreview()
. This project has what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With