When developing camera and SurfaceView as camera programs, you need to obtain the Photo size supported by camera. The getSupportedPictureSizes function is not available in earlier sdk versions. What should I do? refer to the following key code:
1. Define the Size class
[Java] public class Size {
/***
* Sets the dimensions for pictures.
*
* @ Param w the photo width (pixels)
* @ Param h the photo height (pixels)
*/
Public Size (int w, int h ){
Width = w;
Height = h;
}
/***
* Compares {@ code obj} to this size.
*
* @ Param obj the object to compare this size.
* @ Return {@ code true} if the width and height of {@ code obj} is
* Same as those of this size. {@ code false} otherwise.
*/
@ Override
Public boolean equals (Object obj ){
If (! (Obj instanceof Size )){
Return false;
}
Size s = (Size) obj;
Return width = s. width & height = s. height;
}
@ Override
Public int hashCode (){
Return width * 32713 + height;
}
/*** Width of the picture */
Public int width;
/*** Height of the picture */
Public int height;
}
Public class Size {
/***
* Sets the dimensions for pictures.
*
* @ Param w the photo width (pixels)
* @ Param h the photo height (pixels)
*/
Public Size (int w, int h ){
Width = w;
Height = h;
}
/***
* Compares {@ code obj} to this size.
*
* @ Param obj the object to compare this size.
* @ Return {@ code true} if the width and height of {@ code obj} is
* Same as those of this size. {@ code false} otherwise.
*/
@ Override
Public boolean equals (Object obj ){
If (! (Obj instanceof Size )){
Return false;
}
Size s = (Size) obj;
Return width = s. width & height = s. height;
}
@ Override
Public int hashCode (){
Return width * 32713 + height;
}
/*** Width of the picture */
Public int width;
/*** Height of the picture */
Public int height;
}
2. Define the CameraHelper class
[Java] import java. util. ArrayList;
Import java. util. List;
Import java. util. StringTokenizer;
Import android. hardware. Camera;
Import java. util. ArrayList;
Import java. util. List;
Import java. util. StringTokenizer;
Import android. hardware. Camera; [java] public class CameraHelper {
Public class CameraHelper {[java] private static final String KEY_PREVIEW_SIZE = "preview-size ";
Private static final String KEY_PICTURE_SIZE = "picture-size ";
Private static final String SUPPORTED_VALUES_SUFFIX = "-values ";
Private Camera. Parameters mParms;
Public CameraHelper (Camera. Parameters parm ){
This. mParms = parm;
}
Public List <Size> GetSupportedPreviewSizes (){
String str = mParms. get (KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX );
Return splitSize (str );
}
Public List <Size> GetSupportedPictureSizes (){
String str = mParms. get (KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX );
Return splitSize (str );
}
Private ArrayList <Size> splitSize (String str ){
If (str = null)
Return null;
StringTokenizer tokenizer = new StringTokenizer (str ,",");
ArrayList <Size> sizeList = new ArrayList <Size> ();
While (tokenizer. hasMoreElements ()){
Size size = strToSize (tokenizer. nextToken ());
If (size! = Null)
SizeList. add (size );
}
If (sizeList. size () = 0)
Return null;
Return sizeList;
}
Private Size strToSize (String str ){
If (str = null)
Return null;
Int pos = str. indexOf ('x ');
If (pos! =-1 ){
String width = str. substring (0, pos );
String height = str. substring (pos + 1 );
Return new Size (Integer. parseInt (width), Integer. parseInt (height ));
}
Return null;
}
}
Private static final String KEY_PREVIEW_SIZE = "preview-size ";
Private static final String KEY_PICTURE_SIZE = "picture-size ";
Private static final String SUPPORTED_VALUES_SUFFIX = "-values ";
Private Camera. Parameters mParms;
Public CameraHelper (Camera. Parameters parm ){
This. mParms = parm;
}
Public List <Size> GetSupportedPreviewSizes (){
String str = mParms. get (KEY_PREVIEW_SIZE + SUPPORTED_VALUES_SUFFIX );
Return splitSize (str );
}
Public List <Size> GetSupportedPictureSizes (){
String str = mParms. get (KEY_PICTURE_SIZE + SUPPORTED_VALUES_SUFFIX );
Return splitSize (str );
}
Private ArrayList <Size> splitSize (String str ){
If (str = null)
Return null;
StringTokenizer tokenizer = new StringTokenizer (str ,",");
ArrayList <Size> sizeList = new ArrayList <Size> ();
While (tokenizer. hasMoreElements ()){
Size size = strToSize (tokenizer. nextToken ());
If (size! = Null)
SizeList. add (size );
}
If (sizeList. size () = 0)
Return null;
Return sizeList;
}
Private Size strToSize (String str ){
If (str = null)
Return null;
Int pos = str. indexOf ('x ');
If (pos! =-1 ){
String width = str. substring (0, pos );
String height = str. substring (pos + 1 );
Return new Size (Integer. parseInt (width), Integer. parseInt (height ));
}
Return null;
}
}
In fact, it mainly uses the get method of Camera. Parameters, and then parses the obtained string to replace those non-existent APIs.
From the pure soul