展会信息港展会大全

Android 在低版本sdk中没有getSupportedPreviewSizes和getSupportedPictureSizes函
来源:互联网   发布日期:2016-01-14 15:31:49   浏览:1468次  

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

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

1、定义Size类

[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 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 * 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 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 * 32713 + height;

}

/*** width of the picture */

public int width;

/*** height of the picture */

public int height;

}

2、定义CameraHelper类

[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;

}

}

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

摘自 心灵净土

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港