Android调用camera错误setParameters failed深层解析
1. Camera
Camera是Android framework里面支持的,同意你拍照和拍摄视频的设备,那么,在使用camera开发中总是会遇到一些问题,比例如以下面这样子的:
E/AndroidRuntime(1542): java.lang.RuntimeException: setParameters failed
E/AndroidRuntime(1542): at android.hardware.Camera.native_setParameters(Native Method)
E/AndroidRuntime(1542): at android.hardware.Camera.setParameters(Camera.java:914)
出现这样的错误。依据错误提示我们能够知道是android的setParameters方法出错了。
2、那该怎样解决呢?
我们知道camera的parameters中也有非常多參数设置的,是哪个出错了呢?非常多人不知所以然就上网開始找,找不到就開始各种推測,一个个參数设置过去,事实上最有效的方式是究竟层找原因。ok,让我们打开android代码找到camera类。然后查找setParameters方法。
private native final void native_setParameters(String params);
/**
* Changes the settings for this Camera service.
*
* @param params the Parameters to use for this Camera service
* @throws RuntimeException if any parameter is invalid or not supported.
* @see #getParameters()
*/
public void setParameters(Parameters params) {
native_setParameters(params.flatten());
}
从这段代码中代码中。我们能够得到什么信息呢,setParameters方法是调用jni方法native_setParameters的方法,事实上看到这里就差并不多了,由于再去查看jni方法是非常麻烦的。毕竟我们日常开发使用大部分是java代码。我们能够发现传输进来的是Parameters參数,调用了Parameters的flatten方法。
我们查找flatten的代码进行查看。
/**
* Creates a single string with all the parameters set in
* this Parameters object.
* <p>The {@link #unflatten(String)} method does the reverse.</p>
*
* @return a String with all values from this Parameters object, in
* semi-colon delimited key-value pairs
*/
public String flatten() {
StringBuilder flattened = new StringBuilder();
for (String k : mMap.keySet()) {
flattened.append(k);
flattened.append("=");
flattened.append(mMap.get(k));
flattened.append(";");
}
// chop off the extra semicolon at the end
flattened.deleteCharAt(flattened.length()-1);
return flattened.toString();
}
从这段代码中,我们又能得到什么信息呢。我们能够看到提供数据的时候,数据都是从mMap中获取的。ok,接下来,我们查看一下mMap是有几个方法对其进行了赋值呢。
/**
* Takes a flattened string of parameters and adds each one to
* this Parameters object.
* <p>The {@link #flatten()} method does the reverse.</p>
*
* @param flattened a String of parameters (key-value paired) that
* are semi-colon delimited
*/
public void unflatten(String flattened) {
mMap.clear(); StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
while (tokenizer.hasMoreElements()) {
String kv = tokenizer.nextToken();
int pos = kv.indexOf('=');
if (pos == -1) {
continue;
}
String k = kv.substring(0, pos);
String v = kv.substring(pos + 1);
mMap.put(k, v);
}
} /**
* Sets a String parameter.
*
* @param key the key name for the parameter
* @param value the String value of the parameter
*/
public void set(String key, String value) {
if (key.indexOf('=') != -1 || key.indexOf(';') != -1) {
Log.e(TAG, "Key \"" + key + "\" contains invalid character (= or ;)");
return;
}
if (value.indexOf('=') != -1 || value.indexOf(';') != -1) {
Log.e(TAG, "Value \"" + value + "\" contains invalid character (= or ;)");
return;
} mMap.put(key, value);
} /**
* Sets an integer parameter.
*
* @param key the key name for the parameter
* @param value the int value of the parameter
*/
public void set(String key, int value) {
mMap.put(key, Integer.toString(value));
} private void set(String key, List<Area> areas) {
if (areas == null) {
set(key, "(0,0,0,0,0)");
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < areas.size(); i++) {
Area area = areas.get(i);
Rect rect = area.rect;
buffer.append('(');
buffer.append(rect.left);
buffer.append(',');
buffer.append(rect.top);
buffer.append(',');
buffer.append(rect.right);
buffer.append(',');
buffer.append(rect.bottom);
buffer.append(',');
buffer.append(area.weight);
buffer.append(')');
if (i != areas.size() - 1) buffer.append(',');
}
set(key, buffer.toString());
}
} /**
* Returns the value of a String parameter.
*
* @param key the key name for the parameter
* @return the String value of the parameter
*/
public String get(String key) {
return mMap.get(key);
} /**
* Sets the dimensions for preview pictures. If the preview has already
* started, applications should stop the preview first before changing
* preview size.
*
* The sides of width and height are based on camera orientation. That
* is, the preview size is the size before it is rotated by display
* orientation. So applications need to consider the display orientation
* while setting preview size. For example, suppose the camera supports
* both 480x320 and 320x480 preview sizes. The application wants a 3:2
* preview ratio. If the display orientation is set to 0 or 180, preview
* size should be set to 480x320. If the display orientation is set to
* 90 or 270, preview size should be set to 320x480. The display
* orientation should also be considered while setting picture size and
* thumbnail size.
*
* @param width the width of the pictures, in pixels
* @param height the height of the pictures, in pixels
* @see #setDisplayOrientation(int)
* @see #getCameraInfo(int, CameraInfo)
* @see #setPictureSize(int, int)
* @see #setJpegThumbnailSize(int, int)
*/
public void setPreviewSize(int width, int height) {
String v = Integer.toString(width) + "x" + Integer.toString(height);
set(KEY_PREVIEW_SIZE, v);
}
/**
* <p>Sets the dimensions for EXIF thumbnail in Jpeg picture. If
* applications set both width and height to 0, EXIF will not contain
* thumbnail.</p>
*
* <p>Applications need to consider the display orientation. See {@link
* #setPreviewSize(int,int)} for reference.</p>
*
* @param width the width of the thumbnail, in pixels
* @param height the height of the thumbnail, in pixels
* @see #setPreviewSize(int,int)
*/
public void setJpegThumbnailSize(int width, int height) {
set(KEY_JPEG_THUMBNAIL_WIDTH, width);
set(KEY_JPEG_THUMBNAIL_HEIGHT, height);
} /**
* Sets the quality of the EXIF thumbnail in Jpeg picture.
*
* @param quality the JPEG quality of the EXIF thumbnail. The range is 1
* to 100, with 100 being the best.
*/
public void setJpegThumbnailQuality(int quality) {
set(KEY_JPEG_THUMBNAIL_QUALITY, quality);
} /**
* Sets Jpeg quality of captured picture.
*
* @param quality the JPEG quality of captured picture. The range is 1
* to 100, with 100 being the best.
*/
public void setJpegQuality(int quality) {
set(KEY_JPEG_QUALITY, quality);
} /**
* Sets the rate at which preview frames are received. This is the
* target frame rate. The actual frame rate depends on the driver.
*
* @param fps the frame rate (frames per second)
* @deprecated replaced by {@link #setPreviewFpsRange(int,int)}
*/
@Deprecated
public void setPreviewFrameRate(int fps) {
set(KEY_PREVIEW_FRAME_RATE, fps);
} /**
* Sets the maximum and maximum preview fps. This controls the rate of
* preview frames received in {@link PreviewCallback}. The minimum and
* maximum preview fps must be one of the elements from {@link
* #getSupportedPreviewFpsRange}.
*
* @param min the minimum preview fps (scaled by 1000).
* @param max the maximum preview fps (scaled by 1000).
* @throws RuntimeException if fps range is invalid.
* @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
* @see #getSupportedPreviewFpsRange()
*/
public void setPreviewFpsRange(int min, int max) {
set(KEY_PREVIEW_FPS_RANGE, "" + min + "," + max);
} /**
* Sets the image format for preview pictures.
* <p>If this is never called, the default format will be
* {@link android.graphics.ImageFormat#NV21}, which
* uses the NV21 encoding format.</p>
*
* @param pixel_format the desired preview picture format, defined
* by one of the {@link android.graphics.ImageFormat} constants.
* (E.g., <var>ImageFormat.NV21</var> (default),
* <var>ImageFormat.RGB_565</var>, or
* <var>ImageFormat.JPEG</var>)
* @see android.graphics.ImageFormat
*/
public void setPreviewFormat(int pixel_format) {
String s = cameraFormatForPixelFormat(pixel_format);
if (s == null) {
throw new IllegalArgumentException(
"Invalid pixel_format=" + pixel_format);
} set(KEY_PREVIEW_FORMAT, s);
} /**
* <p>Sets the dimensions for pictures.</p>
*
* <p>Applications need to consider the display orientation. See {@link
* #setPreviewSize(int,int)} for reference.</p>
*
* @param width the width for pictures, in pixels
* @param height the height for pictures, in pixels
* @see #setPreviewSize(int,int)
*
*/
public void setPictureSize(int width, int height) {
String v = Integer.toString(width) + "x" + Integer.toString(height);
set(KEY_PICTURE_SIZE, v);
} /**
* Sets the image format for pictures.
*
* @param pixel_format the desired picture format
* (<var>ImageFormat.NV21</var>,
* <var>ImageFormat.RGB_565</var>, or
* <var>ImageFormat.JPEG</var>)
* @see android.graphics.ImageFormat
*/
public void setPictureFormat(int pixel_format) {
String s = cameraFormatForPixelFormat(pixel_format);
if (s == null) {
throw new IllegalArgumentException(
"Invalid pixel_format=" + pixel_format);
} set(KEY_PICTURE_FORMAT, s);
}
ok,错误的地方。我们就定位在这么几个地方了。在自己写的代码里面,查看一下是否调用了这几个方法~~~。android源代码还是凝视得比較清晰的,看看方法英文说明,看是否參数有出现了错误。
当时我的是使用setPictureSize时出现了错误,依据方法说明,我简单解释下。为什么会出错。由于parameters.setPictureSize(320,
480)(设置分辨率)的參数有误,假设不清楚分辨率能够却掉这句话。再执行就OK了。
注:最后找了一下原因,感觉非常easy,在实际 开发中。有时候一个小问题。就让人忙乎一个下午也是正常滴。
Android调用camera错误setParameters failed深层解析的更多相关文章
- Android调用 .Net Core WebApi 返回数据,用FastJSON解析一直报错。
问题描述:.Net Core WebApi中用Newtonsoft.Json 把datatable转成json字符串,如:JsonConvert.SerializeObject(table,Forma ...
- 介绍 Android 的 Camera 框架
总体介绍 Android Camera 框架从整体上看是一个 client/service 的架构,有两个进程:一个是 client 进 程,可以看成是 AP 端,主要包括 JAVA 代码与一些 na ...
- android camera setParameters failed 类问题分析总结
在 monkey test 测试中出现了一例 RuntimeException ,即 setParameters failed. LOG显示为:09-01 18:47:17.348 15656 156 ...
- 解决Android调用相机拍照,要报“打开相机失败”查看debug日志显示“setParameters failed”的问题
使用CameraLibrary项目,在部分手机或平板上不能正常使用,要报“打开相机失败”查看debug日志显示“setParameters failed”. 找到CameraView.java中的se ...
- Android 启动模拟器是出现“Failed to allocate memory: 8”错误提示的原因及解决办法
某天,Android 启动模拟器是出现“Failed to allocate memory: 8”错误,模拟器无法启动,如下图: 原因:设置了不正确AVD显示屏模式,4.0版默认的模式为WVGA800 ...
- Android 自定义Camera 随笔
一.权限 <uses-permission android:name="android.permission.CAMERA" /> <uses-permiss ...
- Android调用系统相机以及自定义相机
0.综述 自定义相机,此处展示简单的相机功能,官方文档中还有相应关于视频拍摄的内容,此处不提 1.添加权限 <!--相机权限,数据存储--> <uses-permission and ...
- Android开发常见错误汇总
[错误信息] [2011-01-19 16:39:10 - ApiDemos] WARNING: Application does not specify an API level requireme ...
- Android图片加载框架最全解析(四),玩转Glide的回调与监听
大家好,今天我们继续学习Glide. 在上一篇文章当中,我带着大家一起深入探究了Glide的缓存机制,我们不光掌握了Glide缓存的使用方法,还通过源码分析对缓存的工作原理进行了了解.虽说上篇文章和本 ...
随机推荐
- HDU 1233 还是畅通工程【最小生成树】
解题思路:kruskal算法:贪心选取最短的边构成一棵最小的生成树 共n个点,即先将所有的边排序,然后利用并查集判断,如果两点连通,则不加入树,不连通,则加入树,直到加入了n-1条边,构成生成树. 反 ...
- XShell与虚拟机连接的IP问题
这几天在Xshell连接虚拟机这个问题上头疼了好长时间,原因是我在虚拟机内的eth0网卡没有分配IP地址,从而导致无法连接XShell,今天解决了这个问题,做一下记录. 首先我使用的是微软的Hyper ...
- swift 20 - Nested Types
Nested Types 只是为了方便类型的整合和使用 struct BlackjackCard { // nested Suit enumeration enum Suit: Character { ...
- 织梦(dedecms)循环调用多级子栏目如二级栏目下三级栏目
本文是关于织梦DedeCMS调用多级子栏目的,拿来分享下. 后台已经建好栏目,对于产品展示栏 栏目导航如下图所示: 复制代码 代码如下: {dede:channelartlist cacheid=' ...
- Ubuntu上面安装docker
1.先用uname -r查看系统的信息 2.安装docker的命令为 sudo apt install docker.io 3.然后就能实现安装了 但是:运行docker search golang, ...
- Windows下从源代码编译Skia
在PPAPI里面画图,能够结合第三方的图形库.比方Cairo.Skia. Google Chrome.Chromium和Android都使用Skia作为画图引擎.我也来试试Skia,先过编译关. fo ...
- DM8168 屏蔽 PCIe
DM8168新板在没有做PCI接口的情况下,能够把PCI模块凝视,直接尝试: make CROSS_COMPILE=arm-none-linux-gnueabi- ARCH=arm menuconfi ...
- [Android Studio 权威教程]配置出“NB”的Android Studio
前几篇博客我们已经安装好了As,并且创建了我们的第一个HelloWrod ,这片blog我们继续配置出一个NB的Android Studio 假设你是一个才開始接触到AS或者想从Eclipse转型到A ...
- bzoj1026: [SCOI2009]windy数(传说你是数位DP)
1026: [SCOI2009]windy数 题目:传送门 题解: 其实之前年少无知的时候好像A过...表示当时并不知道什么数位DP 今天回来深造一发... 其实如果对这个算法稍有了解...看到这题的 ...
- POJ1284 Primitive Roots (原根)
题目链接:http://poj.org/problem?id=1284 题目描述: 题目大意: 一个质数原根的个数 题解: 结论题 一个数n的原根的个数等于$\varphi(\varphi(n))$ ...