首页 技术 正文
技术 2022年11月7日
0 收藏 340 点赞 604 浏览 1982 个字

在做camera和SurfaceView做摄像头程序时,需要获取camera支持的相片大小,在低版本sdk中没有getSupportedPictureSizes函数,怎么办呢,请参阅下面的关键代码:

1、定义Size类

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 with. * @return {@code true} if the width and height of {@code obj} is the * 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 * + height;
} /*** width of the picture */
public int width; /*** height of the picture */
public int height;
}

2、定义CameraHelper类

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import android.hardware.Camera;
public class CameraHelper {
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() == )
return null;
return sizeList;
} private Size strToSize(String str) {
if (str == null)
return null;
int pos = str.indexOf('x');
if (pos != -) {
String width = str.substring(, pos);
String height = str.substring(pos + );
return new Size(Integer.parseInt(width), Integer.parseInt(height));
}
return null;
}}

其实,主要是通过Camera.Parameters的get方法,然后再解析获取的字符串来代替那些不存在的api。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,701
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741