gpt4 book ai didi

java - 如何让 android 相机预览保持专注?

转载 作者:行者123 更新时间:2023-11-30 11:37:23 26 4
gpt4 key购买 nike

我是 Android 的新手,所以代码不是我的,因为我只是用它来学习。
我要制作的应用程序必须显示相机预览。我在表面上有相机的预览,但它没有对焦。作为变通方法,我制作了一个每 750 毫秒调用一次自动对焦的可运行程序。

我想知道如何才能让它变得更好,因为每当我调用自动对焦时,预览都会模糊,直到它再次聚焦。

感谢您的帮助。

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class Draw extends Activity {
private SurfaceView preview = null;
private SurfaceHolder previewHolder = null;
private Camera camera = null;
private boolean inPreview = false;
private boolean cameraConfigured = false;
private Handler handler = new Handler();;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draw);

preview = (SurfaceView) findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.addCallback(surfaceCallback);

final Runnable r = new Runnable() {
@Override
public void run() {
camera.autoFocus(autoFocusCallback);
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 750);
}

private AutoFocusCallback autoFocusCallback = new AutoFocusCallback() {

@Override
public void onAutoFocus(boolean autoFocusSuccess, Camera arg1) {
}
};

@Override
public void onResume() {
super.onResume();

camera = Camera.open();
startPreview();
}

@Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}

camera.release();
camera = null;
inPreview = false;

super.onPause();
}

private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
Camera.Size result = null;

for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;

if (newArea > resultArea) {
result = size;
}
}
}
}
return (result);
}

private void initPreview(int width, int height) {
if (camera != null && previewHolder.getSurface() != null) {
try {
camera.setPreviewDisplay(previewHolder);
} catch (Throwable t) {
Log.e("KritzelKunst-surfaceCallback", "Exception in setPreviewDisplay()", t);
Toast.makeText(Draw.this, t.getMessage(), Toast.LENGTH_LONG).show();
}

if (!cameraConfigured) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);

if (size != null) {
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
cameraConfigured = true;
}
}
}
}

private void startPreview() {
if (cameraConfigured && camera != null) {
camera.startPreview();
inPreview = true;
}
}

SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// no-op -- wait until surfaceChanged()
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
initPreview(width, height);
startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};
}

最佳答案

尝试添加

<uses-feature android:name="android.hardware.camera.autofocus" />

在您的 list 中。

关于java - 如何让 android 相机预览保持专注?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077113/

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