Android camera2 tap to focus - android

Trying to implement tap to focus using camera2api.
CaptureRequest.Builder afBuilder = mPreviewBuilder;
Rect newRect=new Rect(0,0,200,200);
MeteringRectangle meteringRectangle=new MeteringRectangle(newRect,METERING_WEIGHT_DONT_CARE);
MeteringRectangle[] areas = afBuilder.get(CaptureRequest.CONTROL_AF_REGIONS);
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS,areas);
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_AUTO);
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), mCaptureCallback, mBackgroundHandler);
In my call back I am continuously getting stuck in an ACTIVE_SCAN state and occasionally goes into FOCUS_NOT_LOCKED state. I can never seem to get into a FOCUS_LOCKED state and the preview never look focused.
Using a samsung galaxy note 3.

For one, you're not actually setting an AF region - you're just reusing the default region from mPreviewBuilder.
Second, even if you set the region to [(0,0,200,200), METERING_WEIGHT_DONT_CARE], that's the top-left corner of the image, and probably not what you want?
Third, and most importantly, you're setting the AF trigger to be repeating. This means that on every frame, you're asking the camera to restart focusing. So it will never complete, because you never let it.
You need to set AF_TRIGGER to START for only a single capture; you'll still want to set the AF_REGION and AF_MODE on repeating request to be consistent through the whole AF scan you're starting.

Related

Adjustable but consistent/raw feed from Camera X (no AE or AWB)

*Note: I figured out why the image was too dark at max SENSOR_SENSITIVITY: it was just a matter of EXPOSURE_TIME being too short, had to pump it up to 8 digits. Now all I have to do is save the values for SENSOR_SENSITIVITY and EXPOSURE_TIME. So, this question is answered, unless there's a better way?
Using Camera X (Camera2Interop) I want to turn off auto-exposure and white balance. Because I want to manually adjust the brightness, and ideally have it locked at that value until I adjust it again.
I'm not capturing an image but just working with the preview, and image analysis.
When turning CONTROL_MODE off or AE off the preview & analysis are black (unless pointed directly at light bulb), and I can get an image by adjusting SENSOR_SENSITIVITY, but the image is still too dark at maximum and has noise in it. When I modify EXPOSURE_TIME the image goes 100% black no matter what.*
Using AE_LOCK + AE_EXPOSURE_COMPENSATION instead fixes the issue of the black preview and prevents auto-correction, but isn't consistent, for I don't have a way to reset the brightness to what was set previously. For example: if I open the main camera app and go back to mine the brightness is now locked at a different value.
Camera2Interop.Extender extender = new Camera2Interop.Extender(builder);
//extender.setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF);
//extender.setCaptureRequestOption(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION,-1);
extender.setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_MACRO);
//extender.setCaptureRequestOption(CaptureRequest.SENSOR_EXPOSURE_TIME,10);
//extender.setCaptureRequestOption(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF);
//extender.setCaptureRequestOption(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
extender.setCaptureRequestOption(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_OFF);
//extender.setCaptureRequestOption(CaptureRequest.SENSOR_SENSITIVITY,99999);
extender.setCaptureRequestOption(CaptureRequest.CONTROL_AWB_LOCK,true);
extender.setCaptureRequestOption(CaptureRequest.CONTROL_AE_LOCK,true);
//extender.setCaptureRequestOption(CaptureRequest.BLACK_LEVEL_LOCK,true);
Another issue is When adjusting brightness via EXPOSURE_COMPENSATION it seems to compound. For example, if it's set to -3 the image gets a little darker with every start of the application, or probably every time startCameraX is run. Perhaps this is an easy fix by moving it out of startcamera, but I was attempting to start or reset to it's previously set value.
ExposureState exposureState = camera.getCameraInfo().getExposureState();
if (!exposureState.isExposureCompensationSupported()) return;
Range<Integer> range = exposureState.getExposureCompensationRange();
int index = exposureState.getExposureCompensationIndex();
if (range.contains(1) && index != -3) {
camera.getCameraControl().setExposureCompensationIndex(-3);
}
Also, all the warning that come along with the camera2 interop extender makes it seem odd that's the official solution.
Other failed attempts: Canceling focus and metering, and set the metering point to 0x0 pixels.
camera.getCameraControl().cancelFocusAndMetering();//.setExposureCompensationIndex(12);
MeteringPointFactory meteringPointFactory = previewView.getMeteringPointFactory();
MeteringPoint meteringPoint = meteringPointFactory.createPoint(0,0, 0);
FocusMeteringAction action = new FocusMeteringAction.Builder(meteringPoint).build();
//.setAutoCancelDuration(1, TimeUnit.MICROSECONDS).build();
cameraControl.startFocusAndMetering(action);

Explicitly changing CameraX focus while the camera/preview is running? (Not just when building the camera.)

If you want to set the focus to a certain value (say, 10f) while building a CameraX camera, it's pretty easy ...
normally ...
previewBuilder = new Preview.Builder();
preview = previewBuilder.build();
launch camera with a fixed focus ...
previewBuilder = new Preview.Builder();
Camera2Interop.Extender x = new Camera2Interop.Extender(previewBuilder);
x.setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF);
x.setCaptureRequestOption(CaptureRequest.LENS_FOCUS_DISTANCE, 10f);
preview = previewBuilder.build();
The camera and preview will now run, with the focus fixed at "10f".
But in CameraX how do you actually change the focus to some new value, say 5f
So for example using a slider to control the focus - while the camera is actually running.
I am aware you can tap to focus, but I wanna be able to give it a value, such as "5f".
BTW I tried changing the value, on the extended builder, while the camera is running (by hanging on to the builder), which wouldn't make any sense, but in fact you can't do it anyway, you just get an error.
I investigated FocusMeteringAction and it only allows for tap-to-focus; it does not have a "set the value" function.
Any ideas?

camera2 : Problems with focused area and focus distance

I tried many time to set a distance focus on camera2 API, or set autofocus just on a specific area initialized at the start of the activity.
But it still not working...
Could you help me please ? :)
here is my configuration :
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_START);
MeteringRectangle[] focusArea = new MeteringRectangle[1];
focusArea[0] = new MeteringRectangle(new Rect(rectangle.getLeft(),rectangle.getTop(),
rectangle.getRight(), rectangle.getBottom()), MeteringRectangle.METERING_WEIGHT_MAX);
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, focusArea);
//captureRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE,10000000000.0f);
You don't include the code where you call either CameraCaptureSession.capture or setRepeatingRequest. But you should not set TRIGGER_START in a repeating request, since that'll restart focus on every frame. Only use it in a single capture() call.
Also, did you mean you change CONTROL_AF_MODE, not CONTROL_MODE? The latter doesn't affect the type of autofocus used, and I'm assuming you want to do AF_MODE_AUTO for a touch-to-focus operation.
If you want manual focus, you'd need to set AF_MODE_OFF to disable autofocus.

Android camera : Set metering spot position

I'm trying to implement tap to focus on a Samsung Xcover4 for an industrial application (specific to this device).
The idea is to have the device stuffed into a kind of box with a hole where the camera is, and to use it to scan cards with qr codes on it.
So the "scan zone" is always the same, and should be set when the app start with a tap. Once it's done, camera should always focus on that zone and compute exposure on that spot
I'm using Xzing library, so I hacked the CameraManager manager class a little and it's working ok for the fixed focus zone.
I found the "spot" metering value by dumping native camera parameters, but one thing I can't figure out is how to set its position. I guess it can be done, since samsung does it in the native camera app.
ArrayList<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(new Camera.Area(focusArea, 1000));
cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
cameraParameters.setFocusAreas(focusAreas);
cameraParameters.setMeteringAreas(focusAreas);
cameraParameters.setZoom(0);
cameraParameters.set("metering", "spot");
camera.setParameters(cameraParameters);
camera.autoFocus(autoFocusManager);
Below is the list of natvie camera paremeters.
3dnr=false;
Infinity=Infinity;
antibanding=50hz;
antibanding-values=auto,50hz;
auto-exposure-lock-supported=true;
auto-whitebalance-lock-supported=true;
best-capture=0;
brightness=0;
brightness-max=2;
brightness-min=-2;
burst-capture=0;
burstshot-fps-values=(4,4);
constant-growth-rate-zoom-supported=true;
contrast=auto;
drc=false;
dual_mode=-1;
dualrecording-hint=-1;
dynamic-range-control=off;
effect=none;
effect-available-fps-values=(10000,15000);
effect-values=none,mono,negative,sepia,posterize;
effectrecording-hint=0;
exposure-compensation=0;
exposure-compensation-step=0.1;
fast-fps-mode=-1;
flash-mode=off;
flash-mode-values=off,auto,on,torch;
fnumber-value-denominator=10;
fnumber-value-numerator=19;
focal-length=3.70;
focallength-35mm-value=28;
focallength-value-denominator=100;
focallength-value-numerator=370;
focus-areas=(257,416,263,422,1000);
focus-distances=0.10,1.20,Infinity;
focus-mode=auto;
focus-mode-values=auto,macro,continuous-video,continuous-picture;
hdr-mode=0;
horizontal-view-angle=62.2;
hue=0;
hue-max=2;
hue-min=-2;
imageuniqueid-value=V13LLIA02PM V13LLKB16SA
;
intelligent-mode=-1;
iso=auto;
iso-values=auto,100,200,400,800;
jpeg-quality=96;
jpeg-thumbnail-height=384;
jpeg-thumbnail-quality=100;
jpeg-thumbnail-size-values=512x384,512x288,384x384,320x240,0x0;
jpeg-thumbnail-width=512;
max-exposure-compensation=20;
max-num-detected-faces-hw=16;
max-num-detected-faces-sw=0;
max-num-focus-areas=1;
max-num-metering-areas=0;
max-zoom=30;
maxaperture-value-denominator=100;
maxaperture-value-numerator=185;
metering=center;
metering-areas=;
metering-values=matrix,center,spot;
min-exposure-compensation=-20;
odc=false;
phase-af=off;
phase-af-values=off;
picture-format=jpeg;
picture-format-values=jpeg;
picture-size=4128x3096;
picture-size-values=4128x3096,4128x2322,3264x2448,3264x1836,3088x3088,2160x2160,2048x1536,2048x1152,1920x1080,1440x1080,1280x720,960x720,640x480,320x240;
preferred-preview-size-for-video=1280x720;
preview-format=yuv420sp;
preview-format-values=yuv420sp,yuv420p;
preview-fps-range=15000,30000;
preview-fps-range-values=(15000,15000),(24000,24000),(15000,30000),(30000,30000);
preview-frame-rate=30;
preview-frame-rate-values=15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30;
preview-size=1280x720;
preview-size-values=1280x720,1056x704,960x720,880x720,720x720,720x480,640x480,352x288,320x240,176x144;
rotation=0;
rt-hdr=off;
rt-hdr-values=off;
saturation=0;
saturation-max=2;
saturation-min=-2;
scene-mode=auto;
scene-mode-values=auto,action,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight;
sharpness=0;
sharpness-max=2;
sharpness-min=-2;
smooth-zoom-supported=false;
vertical-view-angle=39.4;
video-frame-format=nv21;
video-size=1920x1080;
video-size-values=1920x1080,1440x1080,1072x1072,1280x720,960x720,800x450,720x480,640x480,480x320,352x288,320x240,176x144;
video-snapshot-supported=true;
video-stabilization-supported=false;
vrmode=-1;
wdr=0;
whitebalance=auto;
whitebalance-values=auto,incandescent,fluorescent,daylight,cloudy-daylight;
zoom=0;
zoom-ratios=100,104,109,114,120,125,131,138,144,151,158,166,174,182,190,200,209,219,229,240,251,263,276,289,303,317,332,348,364,381,400;
zoom-supported=true
Thank you.

Android Camera: fixed lens focus

I'm developing an Android application with a camera-related functionality feature.
First of all, I read a lot of stuff on SO, XDA and so on, then please don't redirect me to other useless posts.
I am trying to implement something like a "fixed focus mode", so that:
I start my application with FOCUS_MODE_AUTO (or something else);
bring into focus an object at an arbitrary distance;
fix the current focus;
move the camera on another object at a different distance which is out of focus.
I tried different solutions, i.e.:
mCamera.cancelAutoFocus() in the AutoFocusCallback to prevent the adjustment of the focus;
set a FocusArea: new Camera.Area(new Rect(-50, -50, 50, 50), 1000) to fix the focus on the current area.
I'm targeting API 20 and I'm working on a Samsung Galaxy S5. On this device, the supported focus modes are:
- auto
- infinity
- macro
- continuous-video
- continuous-picture
The suggestion that I found more frequently is to recompile Android...
"AUTO" mode doesn't mean that the camera continuously focuses - just that when you call the autoFocus command the focus is done automatically with no indication on what result you expect not like "Macro" or "Infinity".
http://developer.android.com/reference/android/hardware/Camera.html#autoFocus(android.hardware.Camera.AutoFocusCallback)
So if you don't have a loop that calls the autoFocus (as many examples do or call it again in the Callback) your focus should stay after it runs once.
If I understand, you want to focus keep the focus of the first object.
Have you tried to change the camera mode to FOCUS_MODE_FIXED after you focus the first object ?
Like that :
Camera.Parameters mParam = mCamera.getParameters();
mParam.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
mCamera.setParameters(mParam);

Categories

Resources