Saturday, December 11, 2010

Get supported picture sizes of Android device's camera: getSupportedPictureSizes()

To read the info of supporte picture size of camera, the method getSupportedPictureSizes() can be used. It return a list of supported picture sizes. This method will always return a list with at least one element.

List of Supported Picture Sizes

In order to access Camera, we have to modify AndroidManifest.xml to grant permission of Camera.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidCameraPictureSizes"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidCameraPictureSizes"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>


Modify main.xml to have a Spinner to display the list.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/supportedpicturesizes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Main Java code
package com.exercise.AndroidCameraPictureSizes;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class AndroidCameraPictureSizes extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner spinnerSupportedPictureSizes = (Spinner)findViewById(R.id.supportedpicturesizes);

Camera camera = Camera.open();
Camera.Parameters cameraParameters = camera.getParameters();
List<Camera.Size> listSupportedPictureSizes = cameraParameters.getSupportedPictureSizes();

List<String> listStrSupportedPictureSizes = new ArrayList<String>();

for (int i=0; i < listSupportedPictureSizes.size(); i++){

String strSize = String.valueOf(i) + " : "
+ String.valueOf(listSupportedPictureSizes.get(i).height)
+ " x "
+ String.valueOf(listSupportedPictureSizes.get(i).width);
listStrSupportedPictureSizes.add(strSize);
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listStrSupportedPictureSizes);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSupportedPictureSizes.setAdapter(adapter);

camera.release();
}
}


Download the files.



No comments: