Accessing the Camera from com.google.android.gms.vision.CameraSource and Increasing/Decreasing the Preview Brightness

那年仲夏 提交于 2019-12-23 18:54:12

问题


I Have to implement in Google Vision API's CameraSource for build the camera and do the face detection on before capturing the image. Now I have faced few issues, So I need to access the camera object from CameraSource.

  1. How could I achieve the increase or Decrease the Camera Preview Brightness using CameraSource?

This is my CameraSource Builder

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

Here I have to try to access/get the camera from mCameraSource object.

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();
                }

            }
        }

but the camera returns null only, And My 2nd Question is how to do brightness option...

if (camera != null) {
     Camera.Parameters params = camera.getParameters();                        
     params.setExposureCompensation(1500);
     camera.setParameters(params);
 }

回答1:


I have to achieve my target like this,

Camera Brightness level is -12 to 12. So minimum brightness level is -12, the Maximum brightness level is 12.

now I have to Customize the CameraSourcePreview constructor like

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;
}

and then I have to implement the dynamic brightness adjustment through the seekbar.

like

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 code

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

finally, I got the solution... :)



来源:https://stackoverflow.com/questions/49752955/accessing-the-camera-from-com-google-android-gms-vision-camerasource-and-increas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!