Android - Camera Preview

42,529

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.

Share:
42,529
david
Author by

david

Updated on March 22, 2020

Comments

  • david
    david over 4 years

    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>
    
  • Nilesh Pawar
    Nilesh Pawar over 10 years
    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?
  • CommonsWare
    CommonsWare over 10 years
    @NileshPawar: I do not know what you mean by "skewed" in this situation. FWIW, my current camera work is at: github.com/commonsguy/cwac-camera
  • Nilesh Pawar
    Nilesh Pawar over 10 years
    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
    Nilesh Pawar over 10 years
    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
    Nilesh Pawar over 10 years
    ------------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
    Nilesh Pawar over 10 years
    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.
  • CommonsWare
    CommonsWare over 10 years
    @NileshPawar: Yes, my old getBestPreviewSize() algorithm was weak.
  • Nilesh Pawar
    Nilesh Pawar over 10 years
    nvm. Your working code helped me spot the problem in my code.