从 com.google.android.gms.vision.CameraSource 和 Increasing/Decreasing 预览亮度访问相机
Accessing the Camera from com.google.android.gms.vision.CameraSource and Increasing/Decreasing the Preview Brightness
我必须在 Google Vision API 的 CameraSource 中实现以构建相机并在捕获图像之前进行人脸检测。现在我遇到了一些问题,所以我需要从 CameraSource 访问相机对象。
- 如何使用 CameraSource 增加或减少相机预览亮度?
这是我的 CameraSource Builder
mCameraSource = new CameraSource.Builder(context, detector)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_FRONT)
.setRequestedFps(30.0f)
.build();
这里我必须尝试 access/get 来自 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
returns 仅空,我的第二个问题是如何进行亮度选项...
if (camera != null) {
Camera.Parameters params = camera.getParameters();
params.setExposureCompensation(1500);
camera.setParameters(params);
}
我必须这样实现我的目标,
相机亮度级别是-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实现动态亮度调节
喜欢
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) {
}
});
搜索栏xml代码
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="24"
android:layout_gravity="center_vertical"/>
终于,我得到了解决方案...:)
我必须在 Google Vision API 的 CameraSource 中实现以构建相机并在捕获图像之前进行人脸检测。现在我遇到了一些问题,所以我需要从 CameraSource 访问相机对象。
- 如何使用 CameraSource 增加或减少相机预览亮度?
这是我的 CameraSource Builder
mCameraSource = new CameraSource.Builder(context, detector)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_FRONT)
.setRequestedFps(30.0f)
.build();
这里我必须尝试 access/get 来自 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
returns 仅空,我的第二个问题是如何进行亮度选项...
if (camera != null) {
Camera.Parameters params = camera.getParameters();
params.setExposureCompensation(1500);
camera.setParameters(params);
}
我必须这样实现我的目标,
相机亮度级别是-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实现动态亮度调节
喜欢
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) {
}
});
搜索栏xml代码
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="24"
android:layout_gravity="center_vertical"/>
终于,我得到了解决方案...:)