java 创建相机视图后,Android 自动对焦不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34490276/
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
Android Auto focus doesn't work after creating camera view
提问by Suji
I am trying to create my own Camera View, I have everything working except the autofocus, I can't seem to figure out why it won't work. Here is my code for CameraView.java
我正在尝试创建自己的相机视图,除了自动对焦外,其他一切都在工作,我似乎无法弄清楚为什么它不起作用。这是我的 CameraView.java 代码
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder surface_Holder;
private Camera main_Camera;
boolean on;
public CameraView(Context context, Camera camera){
super(context);
main_Camera = camera;
main_Camera.setDisplayOrientation(90);
surface_Holder = getHolder();
surface_Holder.addCallback(this);
surface_Holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
public boolean isOn(){
return on;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try{
main_Camera.setPreviewDisplay(holder);
main_Camera.startPreview();
}catch (Exception e){
Log.d("Error", "Canmera error on surfaceCreated" + e.getMessage());
main_Camera.release();
main_Camera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(holder.getSurface()==null){
return;
}
try{
main_Camera.stopPreview();
}catch (Exception e){
}
try{
main_Camera.setPreviewDisplay(surface_Holder);
main_Camera.startPreview();
}catch (IOException e){
Log.d("Error", "Camera error on surfaceChanged " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
main_Camera.setPreviewCallback(null);
main_Camera.stopPreview();
main_Camera.release();
main_Camera= null;
}
}
Inside my manifest I have the following:
在我的清单中,我有以下内容:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
回答by Chris
if you added <uses-feature android:name="android.hardware.camera.autofocus" />
to your manifest it doesn't mean the camera will make autofocus
. It means you give your app the permission to use camera hardware or software that take care of autofocus.
如果您添加<uses-feature android:name="android.hardware.camera.autofocus" />
到清单中,并不意味着相机会制作autofocus
. 这意味着您允许您的应用程序使用负责自动对焦的相机硬件或软件。
The purpose of a declaration is to inform any external entity of the set of hardware and software features on which your application depends.
声明的目的是通知任何外部实体您的应用程序所依赖的硬件和软件功能集。
To set your camera to focus you can add this method to your CameraView
class:
要将您的相机设置为对焦,您可以将此方法添加到您的CameraView
类中:
private void setFocus(String mParameter) {
Camera.Parameters mParameters = mCamera.getParameters();
mParameters.setFocusMode(mParameter);
mCamera.setParameters(mParameters);
}
And then call this method in surfaceChanged()
like this:
然后surfaceChanged()
像这样调用这个方法:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
...//your code here
// Set focus mode to continuous picture
setFocus(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// Start camera preview
mCamera.startPreview();
}
You can choose between these focus parameters
:
您可以在以下选项中进行选择focus parameters
:
String FOCUS_MODE_AUTO Auto-focus mode.
String FOCUS_MODE_CONTINUOUS_PICTURE Continuous auto focus mode intended for taking pictures.
String FOCUS_MODE_CONTINUOUS_VIDEO Continuous auto focus mode intended for video recording.
String FOCUS_MODE_EDOF Extended depth of field (EDOF).
String FOCUS_MODE_FIXED Focus is fixed.
String FOCUS_MODE_INFINITY Focus is set at infinity.
String FOCUS_MODE_MACRO Macro (close-up) focus mode.
String FOCUS_MODE_AUTO 自动对焦模式。
String FOCUS_MODE_CONTINUOUS_PICTURE 用于拍照的连续自动对焦模式。
String FOCUS_MODE_CONTINUOUS_VIDEO 用于视频录制的连续自动对焦模式。
字符串 FOCUS_MODE_EDOF 扩展景深 (EDOF)。
String FOCUS_MODE_FIXED 焦点是固定的。
字符串 FOCUS_MODE_INFINITY 焦点设置为无穷大。
String FOCUS_MODE_MACRO 宏(特写)聚焦模式。
回答by Javad Jafari
//set camera to continually auto-focus
Camera.Parameters params = c.getParameters();
//*EDIT*//params.setFocusMode("continuous-picture");
//It is better to use defined constraints as opposed to String, thanks to AbdelHady
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
c.setParameters(params);
回答by halfpastfour.am
Here are some options:
以下是一些选项:
User arsalank2recommends using "continuous auto focus" as described in this answer. However, it seems that some HTC devices do not support this. Check with:
parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTIN??UOUS_VIDEO)
You could implement an
onSensorChanged
listener and focus with a callback when certain criteria are met, see this answerby Juan Acevedo.Handle each case differently to support the widest range of devices possible. Check what works with different models of different devices as you can't fully rely on what the API level says is implemented.
用户arsalank2建议使用本答案中所述的“连续自动对焦” 。但是,似乎某些 HTC 设备不支持此功能。检查:
parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTIN??UOUS_VIDEO)
你可以实现一个
onSensorChanged
监听器,并使用对焦某些条件时,遇到了一个回调,看到这个答案由胡安·阿塞维多。以不同方式处理每种情况,以支持尽可能广泛的设备。检查哪些适用于不同设备的不同型号,因为您不能完全依赖 API 级别所说的实现。
I would recommend going with option 3 as there seems to be no method that works for every single device.
我建议使用选项 3,因为似乎没有适用于每个设备的方法。