When doing camera and Surfaceview to do webcam program, need to get camera support photo size, in the lower version of the SDK does not have getsupportedpicturesizes function, what to do, see the following key code:
1. Define Size class
Public classSize {/** * Sets the dimensions for pictures. * * @param w the photo width (pixels) * @param h the photo height (pixels)*/ PublicSize (intWinth) {width=W; Height=h; } /** * compares {@code obj} to this size. * * @param obj The object to compare this size with. * @return {@code true} if the width and height of {@code obj} are the * Same as those of this size. {@code false} otherwise. */@Override Publicboolean equals (Object obj) {if(!(obj instanceof Size)) { return false; } Size S=(Size) obj; returnwidth = = S.width && height = =S.height; } @Override Public inthashcode () {returnWidth *32713+height; } /** * Width of the picture*/ Public intwidth; /** * Height of the picture*/ Public intheight;}
2. Define Camerahelper Class
Import Java.util.arraylist;import Java.util.list;import java.util.stringtokenizer;import Android.hardware.Camera; Public classCamerahelper {Private StaticFinal String key_preview_size ="preview-size"; Private StaticFinal String key_picture_size ="picture-size"; Private StaticFinal String Supported_values_suffix ="-values"; Privatecamera.parameters mparms; PublicCamerahelper (camera.parameters parm) { This. mparms =Parm; } PublicList<size>getsupportedpreviewsizes () {String str= Mparms.Get(Key_preview_size +Supported_values_suffix); returnsplitsize (str); } PublicList<size>getsupportedpicturesizes () {String str= Mparms.Get(Key_picture_size +Supported_values_suffix); returnsplitsize (str); } PrivateArraylist<size>splitsize (String str) {if(str = =NULL) return NULL; StringTokenizer Tokenizer=NewStringTokenizer (str,","); ArrayList<Size> sizelist =NewArraylist<size>(); while(Tokenizer.hasmoreelements ()) {Size size=strtosize (Tokenizer.nexttoken ()); if(Size! =NULL) sizelist.add (size); } if(sizelist.size () = =0) return NULL; returnsizelist; } PrivateSize strtosize (String str) {if(str = =NULL) return NULL; intpos = Str.indexof ('x'); if(POS! =-1) {String width= Str.substring (0, POS); String Height= str.substring (pos +1); return NewSize (Integer.parseint (width), integer.parseint (height)); } return NULL; }}
In fact, mainly through the camera.parameters get method, and then parse the obtained string to replace those non-existent API.
What about the getsupportedpreviewsizes and getsupportedpicturesizes functions in the Android low version SDK?