问题描述
我现在用的摄像头API演示了一个稍微修改后的版本,它工作在模拟器。我使用的是反映了类,我发现,在邮寄的群体之一,但我对我的Nexus一个测试(安卓2.2.1)时,仍然出现此错误:
12-29 13:22:04.027:ERROR / QualcommCameraHardware(2145):无效的preVIEW尺寸要求:320x402
12-29 13:22:04.037:DEBUG / AndroidRuntime(16514):关闭虚拟机
12-29 13:22:04.037:WARN / dalvikvm(16514):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d7f0)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):致命异常:主要
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):java.lang.RuntimeException的:setParameters失败
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.hardware.Camera.native_setParameters(本机方法)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.hardware.Camera.setParameters(Camera.java:647)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在com.android.iwasthere preview.surfaceChanged(preview.java:78)。
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.SurfaceView.updateWindow(SurfaceView.java:538)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.View.draw(View.java:6743)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.widget.FrameLayout.draw(FrameLayout.java:352)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.drawChild(ViewGroup.java:1640)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.View.draw(View.java:6743)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.widget.FrameLayout.draw(FrameLayout.java:352)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:1842)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewRoot.draw(ViewRoot.java:1407)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.os.Handler.dispatchMessage(Handler.java:99)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.os.Looper.loop(Looper.java:123)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在android.app.ActivityThread.main(ActivityThread.java:4627)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在java.lang.reflect.Method.invokeNative(本机方法)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在java.lang.reflect.Method.invoke(Method.java:521)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-29 13:22:04.047:ERROR / AndroidRuntime(16514):在dalvik.system.NativeStart.main(本机方法)
相机preview.java
xxx软件包;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Window;
公共类相机preVIEW延伸活动{
私人preVIEW米preVIEW;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
米preVIEW =新的preVIEW(本);
的setContentView(M preVIEW);
}
}
preview.java
xxx软件包;
进口java.io.IOException异常;
进口的java.util.List;
进口android.content.Context;
进口android.hardware.Camera;
进口android.hardware.Camera.Size;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
类preVIEW延伸SurfaceView实现SurfaceHolder.Callback {
SurfaceHolder mHolder;
相机mCamera;
preVIEW(上下文的背景下){
超(上下文);
mHolder = getHolder();
mHolder.addCallback(本);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
公共无效surfaceCreated(SurfaceHolder持有者){
mCamera = Camera.open();
尝试 {
mCamera.set previewDisplay(保持器);
}赶上(IOException异常除外){
mCamera.release();
mCamera = NULL;
}
}
公共无效surfaceDestroyed(SurfaceHolder持有者){
mCamera.stop preVIEW();
mCamera.release();
mCamera = NULL;
}
私人大小getOptimal previewSize(名单<大小>的大小,INT W,INT高){
最终双ASPECT_TOLERANCE = 0.05;
双targetRatio =(双)W / H;
如果(大小== NULL)返回NULL;
大小optimalSize = NULL;
双minDiff = Double.MAX_VALUE;
INT targetHeight = H;
对于(尺寸大小:大小){
双率=(双)size.width / size.height;
如果(Math.abs(比 - targetRatio)> ASPECT_TOLERANCE)继续;
如果(Math.abs(size.height - targetHeight)< minDiff){
optimalSize =大小;
minDiff = Math.abs(size.height - targetHeight);
}
}
如果(optimalSize == NULL){
minDiff = Double.MAX_VALUE;
对于(尺寸大小:大小){
如果(Math.abs(size.height - targetHeight)< minDiff){
optimalSize =大小;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
返回optimalSize;
}
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){
Camera.Parameters参数= mCamera.getParameters();
//改性这里
名单<大小>尺寸= Reflect.getSupportedPictureSizes(参数);
//名单,其中,尺寸和GT;大小= parameters.getSupported previewSizes();
大小optimalSize = getOptimal previewSize(尺寸,W,H);
parameters.set previewSize(optimalSize.width,optimalSize.height);
mCamera.setParameters(参数);
mCamera.start preVIEW();
}
}
Reflect.java
xxx软件包;
进口java.lang.reflect.InvocationTargetException;
进口java.lang.reflect.Method中;
进口的java.util.List;
进口android.hardware.Camera;
进口android.hardware.Camera.Size;
公共类反映{
私有静态方法Parameters_getSupportedPictureSizes;
私有静态方法Parameters_getSupported previewSizes;
静态{
initCompatibility();
};
私有静态无效initCompatibility(){
尝试 {
Parameters_getSupportedPictureSizes = Camera.Parameters.class
.getMethod(getSupportedPictureSizes,新的等级[] {});
/ *的成功,这是一个较新的设备* /
}赶上(NoSuchMethodException nsme){
/ *失败,一定是旧的设备* /
}
尝试 {
Parameters_getSupported previewSizes = Camera.Parameters.class
.getMethod(getSupported previewSizes,新的等级[] {});
/ *的成功,这是一个较新的设备* /
}赶上(NoSuchMethodException nsme){
/ *失败,一定是旧的设备* /
}
}
@燮pressWarnings(未登记)
公共静态列表<大小> getSupportedPictureSizes(Camera.Parameters P){
尝试 {
如果(Parameters_getSupportedPictureSizes!= NULL){
返程(名单<大小>)Parameters_getSupportedPictureSizes
.invoke(对);
} 其他 {
返回null;
}
}赶上(ITE的InvocationTargetException){
/ *如果可能的话解开原始异常* /
Throwable的原因= ite.getCause();
如果(事业的instanceof的RuntimeException){
抛出(RuntimeException的)原因;
}否则,如果(事业的instanceof错误){
扔(错误)的原因;
} 其他 {
抛出新的RuntimeException(ITE);
}
}赶上(IllegalAccessException即){
通信System.err.println(意外+ IE);
返回null;
}
}
@燮pressWarnings(未登记)
公共静态列表<大小> getSupported previewSizes(Camera.Parameters P){
尝试 {
如果(Parameters_getSupported previewSizes!= NULL){
返回(名单<大小>)Parameters_getSupported previewSizes
.invoke(对);
} 其他 {
返回null;
}
}赶上(ITE的InvocationTargetException){
/ *如果可能的话解开原始异常* /
Throwable的原因= ite.getCause();
如果(事业的instanceof的RuntimeException){
抛出(RuntimeException的)原因;
}否则,如果(事业的instanceof错误){
扔(错误)的原因;
} 其他 {
/ *
*意想不到的checked异常;包装和再掷
* /
抛出新的RuntimeException(ITE);
}
}赶上(IllegalAccessException即){
返回null;
}
}
}
您不能随心所欲的使用preVIEW大小。你需要使用Camera.Parameters.getSupported previewSizes()和Camera.Parameters.getSupportedPictureSizes()找到最接近的preVIEW和图像大小为你想要的大小。
I am using a slightly modified version of the Camera API Demo and it works in the emulator. I am using a reflect class I found in one of the mailing groups, but I still get this error when testing on my Nexus One (Android 2.2.1):
12-29 13:22:04.027: ERROR/QualcommCameraHardware(2145): Invalid preview size requested: 320x402
12-29 13:22:04.037: DEBUG/AndroidRuntime(16514): Shutting down VM
12-29 13:22:04.037: WARN/dalvikvm(16514): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): FATAL EXCEPTION: main
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): java.lang.RuntimeException: setParameters failed
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.hardware.Camera.native_setParameters(Native Method)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.hardware.Camera.setParameters(Camera.java:647)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at com.android.iwasthere.Preview.surfaceChanged(Preview.java:78)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.SurfaceView.updateWindow(SurfaceView.java:538)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.View.draw(View.java:6743)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.View.draw(View.java:6743)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.widget.FrameLayout.draw(FrameLayout.java:352)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewRoot.draw(ViewRoot.java:1407)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.os.Looper.loop(Looper.java:123)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at java.lang.reflect.Method.invoke(Method.java:521)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-29 13:22:04.047: ERROR/AndroidRuntime(16514): at dalvik.system.NativeStart.main(Native Method)
CameraPreview.java
package xxx;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CameraPreview extends Activity {
private Preview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mPreview = new Preview(this);
setContentView(mPreview);
}
}
Preview.java
package xxx;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = mCamera.getParameters();
//MODIFIED HERE
List<Size> sizes = Reflect.getSupportedPictureSizes(parameters);
//List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Reflect.java
package xxx;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import android.hardware.Camera;
import android.hardware.Camera.Size;
public class Reflect {
private static Method Parameters_getSupportedPictureSizes;
private static Method Parameters_getSupportedPreviewSizes;
static {
initCompatibility();
};
private static void initCompatibility() {
try {
Parameters_getSupportedPictureSizes = Camera.Parameters.class
.getMethod("getSupportedPictureSizes", new Class[] {});
/* success, this is a newer device */
} catch (NoSuchMethodException nsme) {
/* failure, must be older device */
}
try {
Parameters_getSupportedPreviewSizes = Camera.Parameters.class
.getMethod("getSupportedPreviewSizes", new Class[] {});
/* success, this is a newer device */
} catch (NoSuchMethodException nsme) {
/* failure, must be older device */
}
}
@SuppressWarnings("unchecked")
public static List<Size> getSupportedPictureSizes(Camera.Parameters p) {
try {
if (Parameters_getSupportedPictureSizes != null) {
return (List<Size>) Parameters_getSupportedPictureSizes
.invoke(p);
} else {
return null;
}
} catch (InvocationTargetException ite) {
/* unpack original exception when possible */
Throwable cause = ite.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
} else {
throw new RuntimeException(ite);
}
} catch (IllegalAccessException ie) {
System.err.println("unexpected " + ie);
return null;
}
}
@SuppressWarnings("unchecked")
public static List<Size> getSupportedPreviewSizes(Camera.Parameters p) {
try {
if (Parameters_getSupportedPreviewSizes != null) {
return (List<Size>) Parameters_getSupportedPreviewSizes
.invoke(p);
} else {
return null;
}
} catch (InvocationTargetException ite) {
/* unpack original exception when possible */
Throwable cause = ite.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
} else {
/*
* unexpected checked exception; wrap and re-throw
*/
throw new RuntimeException(ite);
}
} catch (IllegalAccessException ie) {
return null;
}
}
}
You can't use arbitrary preview sizes. You'll need to use Camera.Parameters.getSupportedPreviewSizes() and Camera.Parameters.getSupportedPictureSizes() to find the closest preview and picture size to your desired size.
这篇关于Android 2.2的SDK - setParameters失败对Nexus One的摄像头API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!