16

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>
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
david
  • 2,135
  • 4
  • 24
  • 34

1 Answers1

48

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That works great. Additional question. On Android 2.3 and 3.0 the preview seems to be skewed when we rotate the device. Do you happen to have a solution for this too? – Nilesh Pawar Nov 05 '13 at 18:53
  • 1
    @NileshPawar: I do not know what you mean by "skewed" in this situation. FWIW, my current camera work is at: https://github.com/commonsguy/cwac-camera – CommonsWare Nov 05 '13 at 19:01
  • Oh wow. the Camera-demov9 from your new code solved the issue!! I will obtain a diff of what i was doing different and post it here for the benefit of everyone. Sad that Google documentaion is obscure in these areas. – Nilesh Pawar Nov 05 '13 at 19:40
  • The getBestPreviewSize function is not entirely correct. Example for a configuration as follows this function returns 640x480 as the best preview size causing the preview to be skewed. It should give the nearest good value as 864 x 480. Below is log of the avilable preview sizes , my devices display size and the return value from getBestPreviewSize() function. – Nilesh Pawar Nov 05 '13 at 21:45
  • ------------Available Preview sizes------------------ PREVIEW Widt: 1280 Height: 720 PREVIEW Widt: 864 Height: 480 PREVIEW Widt: 640 Height: 480 PREVIEW Widt: 480 Height: 320 PREVIEW Widt: 352 Height: 288 PREVIEW Widt: 320 Height: 240 PREVIEW Widt: 176 Height: 144 ----------------------------------------- DISPLAY SIZE: Width: 854 Height: 480 GET BEST PREVIEW SIZE FOR : 854 Height: 480 BEST PREVIEW SIZE = Width: 640 Height: 480 – Nilesh Pawar Nov 05 '13 at 21:45
  • I modified my getBestPreviewSize() function to use the distance formula in co-ordinate geometry to calculate the best possible preview size. this one solved the issue above. – Nilesh Pawar Nov 05 '13 at 21:49
  • @NileshPawar: Yes, my old `getBestPreviewSize()` algorithm was weak. – CommonsWare Nov 05 '13 at 21:49
  • nvm. Your working code helped me spot the problem in my code. – Nilesh Pawar Nov 05 '13 at 21:51