Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ internal partial class CameraManager
ProcessCameraProvider cameraProvider;
ICamera camera;

Timer timer;

public NativePlatformCameraPreviewView CreateNativeView()
{
previewView = new PreviewView(Context.Context);
cameraExecutor = Executors.NewSingleThreadExecutor();
cameraExecutor = Executors.NewSingleThreadExecutor();

return previewView;
}
Expand All @@ -63,6 +65,7 @@ public void Connect()
// Frame by frame analyze
imageAnalyzer = new ImageAnalysis.Builder()
.SetDefaultResolution(new Android.Util.Size(640, 480))
.SetOutputImageRotationEnabled(true)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be set based on some input?

I don't want it to auto-rotate if I initialise this:

          new CameraBarcodeReaderView
          {
            Options = new BarcodeReaderOptions
            {
              AutoRotate = false,
              Multiple = false,
            }
          }

.SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest)
.Build();

Expand All @@ -71,9 +74,64 @@ public void Connect()

UpdateCamera();

}), ContextCompat.GetMainExecutor(Context.Context)); //GetMainExecutor: returns an Executor that runs on the main thread.
AutoFocus();
setupAutoFocusTimer();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it adds a continuous autofocus that's never canceled, even when you explicitly call one of the other functions.

((View)previewView.Parent).SetOnTouchListener(new TapFocusTouchListener(this));


}), ContextCompat.GetMainExecutor(Context.Context)); //GetMainExecutor: returns an Executor that runs on the main thread.
}

private class TapFocusTouchListener : Java.Lang.Object, View.IOnTouchListener {

private CameraManager cameraManager;

public TapFocusTouchListener(CameraManager cameraManager)
{
this.cameraManager = cameraManager;
}

public bool OnTouch(View v, MotionEvent e)
{

if (e.Action == MotionEventActions.Down)
{
Point point = new Point(((int)e.GetX()), ((int)e.GetY()));
cameraManager.Focus(point);
return true;
}
return false;
}
}

private void setupAutoFocusTimer()
{
if(timer != null)
{
timer.Cancel();
timer.Dispose();
timer = null;
}
timer = new Timer();
var task = new AFTimerTask(this);
timer.ScheduleAtFixedRate(task, 5000, 5000);
}

private class AFTimerTask : TimerTask
{
private CameraManager cameraManager;

public AFTimerTask(CameraManager manager)
{
this.cameraManager = manager;
}

public override void Run()
{
cameraManager.AutoFocus();
}
}

public void Disconnect()
{ }

Expand Down Expand Up @@ -110,13 +168,30 @@ public void UpdateTorch(bool on)

public void Focus(Point point)
{
camera?.CameraControl?.CancelFocusAndMetering();

}
var factory = new SurfaceOrientedMeteringPointFactory(previewView.LayoutParameters.Width, previewView.LayoutParameters.Height);
var fpoint = factory.CreatePoint(point.X, point.Y);
var action = new FocusMeteringAction.Builder(fpoint, FocusMeteringAction.FlagAf)
.DisableAutoCancel()
.Build();

camera?.CameraControl?.StartFocusAndMetering(action);

}

public void AutoFocus()
{
camera?.CameraControl?.CancelFocusAndMetering();
var factory = new SurfaceOrientedMeteringPointFactory(1f, 1f);
var fpoint = factory.CreatePoint(.5f, .5f);
var action = new FocusMeteringAction.Builder(fpoint,FocusMeteringAction.FlagAf)
//.DisableAutoCancel()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either remove this or uncomment it.

.Build();

}
camera?.CameraControl?.StartFocusAndMetering(action);

}

public void Dispose()
{
Expand Down