创建相机视图后,Android自动对焦不起作用 [英] Android Auto focus doesn't work after creating camera view
问题描述
我正在尝试创建自己的相机视图",除自动对焦功能外,所有其他功能都可以正常工作,我似乎无法弄清楚为什么它不起作用.这是我的CameraView.java代码
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
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" />
推荐答案
如果将<uses-feature android:name="android.hardware.camera.autofocus" />
添加到清单中,并不意味着相机会制作autofocus
.这意味着您已授予您的应用许可,以使用可自动对焦的相机硬件或软件.
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.
声明的目的是将应用程序所依赖的一组硬件和软件功能通知任何外部实体.
The purpose of a declaration is to inform any external entity of the set of hardware and software features on which your application depends.
要将相机设置为焦点,可以将此方法添加到您的CameraView
类中:
To set your camera to focus you can add this method to your CameraView
class:
private void setFocus(String mParameter) {
Camera.Parameters mParameters = mCamera.getParameters();
mParameters.setFocusMode(mParameter);
mCamera.setParameters(mParameters);
}
然后像这样在surfaceChanged()
中调用此方法:
And then call this method in surfaceChanged()
like this:
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();
}
您可以在以下focus parameters
之间进行选择:
You can choose between these focus parameters
:
字符串FOCUS_MODE_AUTO自动对焦模式.
String FOCUS_MODE_AUTO Auto-focus mode.
字符串FOCUS_MODE_CONTINUOUS_PICTURE连续自动对焦模式 用于拍照.
String FOCUS_MODE_CONTINUOUS_PICTURE Continuous auto focus mode intended for taking pictures.
字符串FOCUS_MODE_CONTINUOUS_VIDEO适用于连续自动对焦模式 进行视频录制.
String FOCUS_MODE_CONTINUOUS_VIDEO Continuous auto focus mode intended for video recording.
字符串FOCUS_MODE_EDOF扩展景深(EDOF).
String FOCUS_MODE_EDOF Extended depth of field (EDOF).
字符串FOCUS_MODE_FIXED焦点已固定.
String FOCUS_MODE_FIXED Focus is fixed.
字符串FOCUS_MODE_INFINITY焦点设置为无穷远.
String FOCUS_MODE_INFINITY Focus is set at infinity.
字符串FOCUS_MODE_MACRO宏(特写)聚焦模式.
String FOCUS_MODE_MACRO Macro (close-up) focus mode.
这篇关于创建相机视图后,Android自动对焦不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!