Java 无法连接到相机服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3371692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fail to connect to camera service
提问by Skizit
I had my camera set to this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and it works fine but if I change it to PORTRAIT
instead of LANDSCAPE
then it crashes with the following error...
我的相机设置为this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
并且它工作正常但是如果我将它更改为PORTRAIT
而不是LANDSCAPE
然后它会崩溃并出现以下错误......
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): FATAL EXCEPTION: main
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): java.lang.RuntimeException: Fail to connect to camera service
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at android.hardware.Camera.native_setup(Native Method)
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at android.hardware.Camera.<init>(Camera.java:110)
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at android.hardware.Camera.open(Camera.java:90)
07-30 12:51:37.655: ERROR/AndroidRuntime(22069): at org.digital.com.CamLayer.surfaceCreated(CamLayer.java:3
The method it crashes in is..
它崩溃的方法是..
public void surfaceCreated(SurfaceHolder holder) {
synchronized(this) {
mCamera = Camera.open();
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(800, 480);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e("Camera", "mCamera.setPreviewDisplay(holder);");
}
mCamera.startPreview();
mCamera.setPreviewCallback(this);
}
}
It crashes at mCamera = Camera.open();
它崩溃于 mCamera = Camera.open();
My Manifest file has <uses-permission android:name="android.permission.CAMERA"></uses-permission>
我的清单文件有 <uses-permission android:name="android.permission.CAMERA"></uses-permission>
How do I fix this so I can view my app in Portrait?
如何解决此问题,以便我可以在纵向模式中查看我的应用程序?
采纳答案by Mathias Conradt
For your reference, this is the SurfaceHolderCallBack inner class that I'm using in my app and which works fine on Nexus One 2.2 in portrait mode - hope it helps.
供您参考,这是我在我的应用程序中使用的 SurfaceHolderCallBack 内部类,它在纵向模式下在 Nexus One 2.2 上运行良好 - 希望它有所帮助。
Edit: "which works" = "which doesn't crash". Although I haven't figured out how to rotate the preview image correctly (see my first comment above). That's why I actually had to use landscape and 'convert' UI stuff that's surrounding the preview surface into landscape mode. Afaik preview (with correct rotation of the preview image) only works in landscape mode. Rotation & orientation params that I tried didn't help at all.
编辑:“哪个有效”=“哪个不会崩溃”。虽然我还没有想出如何正确旋转预览图像(见我上面的第一条评论)。这就是为什么我实际上必须使用横向并将预览表面周围的 UI 内容“转换”为横向模式。Afaik 预览(正确旋转预览图像)仅适用于横向模式。我尝试过的旋转和方向参数根本没有帮助。
class SurfaceHolderCallback implements SurfaceHolder.Callback {
private static final int IMAGE_WIDTH = 512;
private static final int IMAGE_HEIGHT = 384;
private static final String ORIENTATION = "orientation";
private static final String ROTATION = "rotation";
private static final String PORTRAIT = "portrait";
private static final String LANDSCAPE = "landscape";
public void surfaceCreated(SurfaceHolder holder) {
try {
// This case can actually happen if the user opens and closes the camera too frequently.
// The problem is that we cannot really prevent this from happening as the user can easily
// get into a chain of activites and tries to escape using the back button.
// The most sensible solution would be to quit the entire EPostcard flow once the picture is sent.
camera = Camera.open();
} catch(Exception e) {
finish();
return;
}
//Surface.setOrientation(Display.DEFAULT_DISPLAY,Surface.ROTATION_90);
Parameters p = camera.getParameters();
p.setPictureSize(IMAGE_WIDTH, IMAGE_HEIGHT);
camera.getParameters().setRotation(90);
Camera.Size s = p.getSupportedPreviewSizes().get(0);
p.setPreviewSize( s.width, s.height );
p.setPictureFormat(PixelFormat.JPEG);
p.set("flash-mode", "auto");
camera.setParameters(p);
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (Throwable ignored) {
Log.e(APP, "set preview error.", ignored);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreviewRunning) {
camera.stopPreview();
}
try {
camera.startPreview();
} catch(Exception e) {
Log.d(APP, "Cannot start preview", e);
}
isPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder arg0) {
if(isPreviewRunning && camera != null) {
if(camera!=null) {
camera.stopPreview();
camera.release();
camera = null;
}
isPreviewRunning = false;
}
}
}
回答by Romain Hippeau
Do you have this set in AndroidManifest.xml ?
你在 AndroidManifest.xml 中有这个设置吗?
uses-permission android:name="android.permission.CAMERA"
回答by the100rabh
I see you have set your preview area set as 800 x 480. In portrait mode, this size is invalid and there might be a bug in the API which makes it crash.
我看到您已将预览区域设置为 800 x 480。在纵向模式下,此大小无效,并且 API 中可能存在导致崩溃的错误。
Try setting a preview area of 200x200
尝试设置 200x200 的预览区域
回答by Kangur
There is some concurrency problem in platform: http://code.google.com/p/android/issues/detail?id=6201
平台存在一些并发问题:http: //code.google.com/p/android/issues/detail?id=6201
The workaround is to clear callback before releasing cam, that is I would recommend following clean-up code:
解决方法是在释放凸轮之前清除回调,即我建议遵循清理代码:
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
回答by Tom Groentjes
I think your problem is that when you rotate the camera it is not released correctly or at all. If you switch from landscape
to portrait
the Intent is launched again. If then the camera is not released and reopend you try to open an already open camera, which throws an error.
我认为您的问题是当您旋转相机时,它没有正确释放或根本没有释放。如果切换landscape
到portrait
Intent 会再次启动。如果那时相机没有被释放并重新打开,您尝试打开一个已经打开的相机,这会引发错误。
回答by Vaibhav
Instead of using :
而不是使用:
<uses-permission
android:name="android.permission.FLASHLIGHT"/>
Try to use:
尝试使用:
<uses-permission
android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
and don't forget to add the permission for camera:
并且不要忘记添加相机权限:
<uses-permission android:name="android.permission.CAMERA" />
回答by Dave Burke
Another possibility if you're using an Android Emulator may be that the front and back cameras are defined as "none" in the Android Virtual Device settings.
如果您使用的是 Android 模拟器,另一种可能性可能是前后摄像头在 Android 虚拟设备设置中被定义为“无”。
回答by MJahongir
this might work...
这可能有效...
@Override public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("surfaceDestroyed", "");
cameraSource.stop(); //check if you have this line
}