Wednesday, August 22, 2012

Determine the best camera preview size

It'a example to determine the best supported camera preview size, to have the biggest preview area.

Determine the best camera preview size


package com.example.androidcamera;

import java.io.IOException;
import java.util.List;

import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {
 
 Camera myCamera;
 SurfaceView mySurfaceView;
 SurfaceHolder mySurfaceHolder;
 boolean isPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        isPreview = false;
        mySurfaceView = (SurfaceView)findViewById(R.id.mypreview);
        mySurfaceHolder = mySurfaceView.getHolder();
        mySurfaceHolder.addCallback(mySurfaceCallback);
        
    }
    
    SurfaceHolder.Callback mySurfaceCallback
    = new SurfaceHolder.Callback(){

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
   Camera.Parameters myParameters = myCamera.getParameters();
   Camera.Size myBestSize = getBestPreviewSize(width, height, myParameters);
   
   if(myBestSize != null){
    myParameters.setPreviewSize(myBestSize.width, myBestSize.height);
    myCamera.setParameters(myParameters);
    myCamera.startPreview();
    isPreview = true;
    
    Toast.makeText(getApplicationContext(), 
      "Best Size:\n" +
      String.valueOf(myBestSize.width) + " : " + String.valueOf(myBestSize.height), 
      Toast.LENGTH_LONG).show();
   }
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
   try {
    myCamera.setPreviewDisplay(mySurfaceHolder);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
   // TODO Auto-generated method stub
   
  }
     
    };
    
    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters){
     Camera.Size bestSize = null;
     List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();
     
     bestSize = sizeList.get(0);
     
     for(int i = 1; i < sizeList.size(); i++){
      if((sizeList.get(i).width * sizeList.get(i).height) >
        (bestSize.width * bestSize.height)){
       bestSize = sizeList.get(i);
      }
     }

     return bestSize;
    }
    
 @Override
 protected void onResume() {
  super.onResume();
  myCamera = Camera.open();
 }

 @Override
 protected void onPause() {
  
  if(isPreview){
   myCamera.stopPreview();
  }
  
  myCamera.release();
  myCamera = null;
  isPreview = false;
  
  super.onPause();
 }

}


Modify the layout to have a SurfaceView for preview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/mypreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>


Modify AndroidManifest.xml to add permission of "android.permission.CAMERA", and set android:screenOrientation="landscape".
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidcamera"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Download the files.


1 comment:

Unknown said...

Ty for this code and tutorial :)