从com.google.android.gms.vision.CameraSource访问相机并增加/减少预览亮度

问题描述 投票:2回答:1

我必须在Google Vision API的CameraSource中实现构建相机并在捕获图像之前进行面部检测。现在我遇到了一些问题,所以我需要从CameraSource访问相机对象。

  1. 如何使用CameraSource实现增加或降低相机预览亮度?

这是我的CameraSource Builder

 mCameraSource = new CameraSource.Builder(context, detector)
                .setRequestedPreviewSize(640, 480)
                .setFacing(CameraSource.CAMERA_FACING_FRONT)
                .setRequestedFps(30.0f)
                .build();

在这里,我必须尝试从mCameraSource对象访问/获取相机。

Field[] declaredFields = CameraSource.class.getDeclaredFields();

        for (Field field : declaredFields) {
            if (field.getType() == Camera.class) {
                field.setAccessible(true);
                try {
                    Camera camera = (Camera) field.get(mCameraSource);
                    if (camera != null) {
                        Camera.Parameters params = camera.getParameters();                        
                        params.setExposureCompensation(1500);
                        camera.setParameters(params);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
        }

但是camera只返回null,而我的第二个问题是如何做亮度选项......

if (camera != null) {
     Camera.Parameters params = camera.getParameters();                        
     params.setExposureCompensation(1500);
     camera.setParameters(params);
 }
java android camera surfaceview vision-api
1个回答
0
投票

我必须达到这样的目标,

相机亮度等级为-12至12.因此最低亮度等级为-12,最高亮度等级为12。

现在我必须自定义CameraSourcePreview构造函数

private int defaultBrightness = 12;

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);
    mSurfaceView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            cameraFocus(mCameraSource, brightness);
        }
    });
}

private static boolean cameraFocus(@NonNull CameraSource cameraSource, int brightness) {
    Field[] declaredFields = CameraSource.class.getDeclaredFields();

    for (Field field : declaredFields) {
        if (field.getType() == Camera.class) {
            field.setAccessible(true);
            try {
                Camera camera = (Camera) field.get(cameraSource);
                if (camera != null) {
                    Camera.Parameters params = camera.getParameters();
                        params.setExposureCompensation(brightness);
                    camera.setParameters(params);
                    return true;
                }

                return false;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            break;
        }
    }

    return false;
}

然后我必须通过搜索栏实现动态亮度调整。

喜欢

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {


                cameraFocus(mCameraSource,i-12);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

seekbar xml代码

<SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="24"
            android:layout_gravity="center_vertical"/>

最后,我得到了解决方案...... :)

© www.soinside.com 2019 - 2024. All rights reserved.