Android sdk in earlier versions does not include getSupportedPreviewSizes and getSupportedPictu

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.