Camera.getParameters() 在 Galaxy Tab 上返回 null
这是我的表面更改事件处理代码:
public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height,
parameters);
//...
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result = null;
// it fails with NullPointerExceptiopn here,
// when accessing "getSupportedPreviewSizes" method:
// that means "parameters" is null
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
///...
}
}
我像这样初始化相机:
@Override
public void onResume() {
super.onResume();
camera = Camera.open();
}
这个问题不会发生在我的 Galaxy S Plus 上,也不会发生在 LG Optimus Black 手机上。有人认为这里出了什么问题吗?
Here is my surface-changed event handling code:
public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height,
parameters);
//...
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result = null;
// it fails with NullPointerExceptiopn here,
// when accessing "getSupportedPreviewSizes" method:
// that means "parameters" is null
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
///...
}
}
I initialize camera like this:
@Override
public void onResume() {
super.onResume();
camera = Camera.open();
}
This problem doesn't occur on my Galaxy S Plus neither it happen on LG Optimus Black phone. Has anyone thoughts what's wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经解决了这个问题。
在 Galaxy Tab 上返回 NULL。因此,我只是检查它是否为空,并且在这种情况下不设置新的预览大小。我在研究了标准相机应用程序源之后得出了这个结论。
I've solved this.
Returns NULL on Galaxy Tab. So I just make a check if it is null and don't set new preview size in such case. To this conclusion I've came after looking into standard Camera application sources.
看起来相机变量从未初始化,因此您正在对 null 调用 getParameters() 。首先尝试调用camera = Camera.open();
Looks like the camera variable was never initialized so you are calling getParameters() on null. Try calling
camera = Camera.open();
first相机初始化很大程度上取决于特定设备。例如,特定的三星设备 GT5500 报告空(宽度 = 0,高度 = 0)作为预览的有效分辨率,但如果您尝试使用它,则会导致整个手机崩溃(“硬”重启)。我们使用 mixare 增强现实引擎(http://www.mixare.org)体验了它,并且需要 PITA 进行调试(因为我们没有手机,无法在任何其他硬件上重现该错误)。
然而,关于获得“正确的”预览尺寸,您可以查看我们在 github 上的代码(它是一个免费的开源应用程序)。在文件中: https://github.com/mixare /mixare/blob/master/src/org/mixare/MixView.java(第 871 行及以后)
如您所见,我们没有直接使用对 Camera 类的 getSupportedPreviewSizes 的调用,而是添加了一个兼容性层(代码在这里: https ://github.com/mixare/mixare/blob/master/src/org/mixare/Compatibility.java )因为我们需要与旧手机兼容。如果您不想支持较旧的 Android 版本,您可以直接使用 Camera 类的方法。
华泰
丹尼尔
camera initialization depends a lot on the specific device. For instance a specific Samsung device GT5500 is reporting null (width = 0, height = 0) as a valid resolution for preview, but crashes the whole phone ("hard" reboot) if you try to use it. We experienced it with mixare augmented reality engine (http://www.mixare.org) and it was PITA to debug (since we didn't have the phone and could not reproduce the bug on any other hardware).
However, about getting the "right" preview size you can take a look at our code (it's a free and open source app) on github. In the file: https://github.com/mixare/mixare/blob/master/src/org/mixare/MixView.java (row 871 and onwards)
As you see we're not using directly the call to getSupportedPreviewSizes of the Camera class, but instead added a compatibility layer (the code is here: https://github.com/mixare/mixare/blob/master/src/org/mixare/Compatibility.java ) because we needed compatibility with older phones. If you don't want to support older android releases you can use the method of the Camera class directly.
HTH
Daniele