gpt4 book ai didi

android - 面部跟踪器 CameraSource Android : How to brighten front camera quality?

转载 作者:太空狗 更新时间:2023-10-29 13:53:58 25 4
gpt4 key购买 nike

基于 Google Vision Face Tracker 的面部追踪器应用程序.默认情况下,Face Tracker 使用后置/后置摄像头,但我想使用前置摄像头检测人脸。

这是google vision提供的CameraSourcePreview的代码:

package com.google.android.gms.samples.vision.face.facetracker.ui.camera;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import com.google.android.gms.common.images.Size;
import com.google.android.gms.vision.CameraSource;

import java.io.IOException;

public class CameraSourcePreview extends ViewGroup {
private static final String TAG = "CameraSourcePreview";

private Context mContext;
private SurfaceView mSurfaceView;
private boolean mStartRequested;
private boolean mSurfaceAvailable;
private CameraSource mCameraSource;

private GraphicOverlay mOverlay;

public CameraSourcePreview(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mStartRequested = false;
mSurfaceAvailable = false;

mSurfaceView = new SurfaceView(context);
mSurfaceView.getHolder().addCallback(new SurfaceCallback());
addView(mSurfaceView);
}

public void start(CameraSource cameraSource) throws IOException {
if (cameraSource == null) {
stop();
}

mCameraSource = cameraSource;

if (mCameraSource != null) {
mStartRequested = true;
startIfReady();
}
}

public void start(CameraSource cameraSource, GraphicOverlay overlay) throws IOException {
mOverlay = overlay;
start(cameraSource);
}

public void stop() {
if (mCameraSource != null) {
mCameraSource.stop();
}
}

public void release() {
if (mCameraSource != null) {
mCameraSource.release();
mCameraSource = null;
}
}

private void startIfReady() throws IOException {
if (mStartRequested && mSurfaceAvailable) {
mCameraSource.start(mSurfaceView.getHolder());
if (mOverlay != null) {
Size size = mCameraSource.getPreviewSize();
int min = Math.min(size.getWidth(), size.getHeight());
int max = Math.max(size.getWidth(), size.getHeight());
if (isPortraitMode()) {
// Swap width and height sizes when in portrait, since it will be rotated by
// 90 degrees
mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
} else {
mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
}
mOverlay.clear();
}
mStartRequested = false;
}
}

private class SurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder surface) {
mSurfaceAvailable = true;
try {
startIfReady();
} catch (IOException e) {
Log.e(TAG, "Could not start camera source.", e);
}
}

@Override
public void surfaceDestroyed(SurfaceHolder surface) {
mSurfaceAvailable = false;
}

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

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int width = 640;
int height = 480;
if (mCameraSource != null) {
Size size = mCameraSource.getPreviewSize();
if (size != null) {
width = size.getWidth();
height = size.getHeight();
}
}

// Swap width and height sizes when in portrait, since it will be rotated 90 degrees
if (isPortraitMode()) {
int tmp = width;
width = height;
height = tmp;
}

final int layoutWidth = right - left;
final int layoutHeight = bottom - top;

// Computes height and width for potentially doing fit width.
int childWidth = layoutWidth;
int childHeight = (int)(((float) layoutWidth / (float) width) * height);

// If height is too tall using fit width, does fit height instead.
if (childHeight > layoutHeight) {
childHeight = layoutHeight;
childWidth = (int)(((float) layoutHeight / (float) height) * width);
}

for (int i = 0; i < getChildCount(); ++i) {
getChildAt(i).layout(0, 0, childWidth, childHeight);
}

try {
startIfReady();
} catch (IOException e) {
Log.e(TAG, "Could not start camera source.", e);
}
}

private boolean isPortraitMode() {
int orientation = mContext.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
return false;
}
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
return true;
}

Log.d(TAG, "isPortraitMode returning false by default");
return false;
}
}

我用这个方法调用相机源:

private void startCameraSource() {

// check that the device has play services available.
int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
getApplicationContext());
if (code != ConnectionResult.SUCCESS) {
Dialog dlg =
GoogleApiAvailability.getInstance().getErrorDialog(this, code, RC_HANDLE_GMS);
dlg.show();
}

if (mCameraSource != null) {
try {
mPreview.start(mCameraSource, mGraphicOverlay);
} catch (IOException e) {
Log.e(TAG, "Unable to start camera source.", e);
mCameraSource.release();
mCameraSource = null;
}
}
}

与默认手机摄像头应用程序相比,Face Tracker 前置摄像头仍然太暗。

如何在面部跟踪器 google vision 中调亮前置摄像头?与表面 View 有关吗?

<com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.00"
android:weightSum="1">

<com.google.android.gms.samples.vision.face.facetracker.ui.camera.GraphicOverlay
android:id="@+id/faceOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.79" />

</com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview>

最佳答案

它与 SurfaceView 完全无关。这是 Camera API 配置错误。您必须在 CameraSource.java 文件中进行一些额外的更改。您可以在 this GitHub repository 上找到它

首先,你需要知道这是一个曝光问题。它是指相机允许在镜头上接收到的光线。您需要知道您的相机是否支持曝光补偿。您将不得不查询 getMinExposureCompensation()getMaxExposureCompensation()来自您的 Camera.Parameters 实例。如文档所述,如果两种方法都返回 0,则不支持曝光补偿,您也无能为力。

幸运的是,大部分时间所有手机都支持此功能。现在您可以通过调用 getExposureCompensation() 查看当前相机曝光这将返回默认值(通常为 0,表示不调整曝光)。现在为了防止图像变暗,您只需使用setExposureCompensation() 将新的曝光设置在最小值和最大值之间。并将 Camera.Parameters 应用于您的相机。

最后,您可以使用 setAutoExposureLock() 锁定曝光以避免丢失配置。 , getAutoExposureLock()最重要的是:在设置曝光锁之前,您必须确保 isAutoExposureLockSupported()返回真。

祝你好运!

关于android - 面部跟踪器 CameraSource Android : How to brighten front camera quality?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41766196/

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