gpt4 book ai didi

java - 创建相机 View 后,Android 自动对焦不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:44 24 4
gpt4 key购买 nike

我正在尝试创建我自己的相机 View ,除了自动对焦之外,我的一切都正常,我似乎无法弄清楚为什么它不起作用。这是我的 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;
}
}

在我的 list 中,我有以下内容:

<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" />对于您的 list ,这并不意味着相机会制作 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类:

private void setFocus(String mParameter) {
Camera.Parameters mParameters = mCamera.getParameters();
mParameters.setFocusMode(mParameter);
mCamera.setParameters(mParameters);
}

然后在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();

}

您可以在这些之间进行选择 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.

关于java - 创建相机 View 后,Android 自动对焦不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34490276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com