Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow android client code to customize preview size. #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion src/ZXing.Net.Mobile/Android/ZXingSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public void SurfaceChanged (ISurfaceHolder holder, global::Android.Graphics.Form
parameters.SetPreviewFpsRange (30000, 30000);
parameters.SetPreviewSize (640, 360);
}

SetPreviewSizePerOptions(parameters);

camera.SetParameters (parameters);

SetCameraDisplayOrientation (this.activity);
Expand All @@ -159,7 +162,6 @@ public void SurfaceDestroyed (ISurfaceHolder holder)
ShutdownCamera ();
}


public byte[] rotateCounterClockwise(byte[] data, int width, int height)
{
var rotatedData = new byte[data.Length];
Expand All @@ -169,6 +171,23 @@ public byte[] rotateCounterClockwise(byte[] data, int width, int height)
}
return rotatedData;
}

private void SetPreviewSizePerOptions (Android.Hardware.Camera.Parameters parameters)
{
if (options.PreviewSizeSelector == null)
{
return;
}
var choices = parameters.SupportedPreviewSizes
.Select(sz => new Dimension(sz.Width, sz.Height))
.ToList();
var selectedChoice = options.PreviewSizeSelector(choices);
if (selectedChoice == null)
{
return;
}
parameters.SetPreviewSize(selectedChoice.Width, selectedChoice.Height);
}

DateTime lastPreviewAnalysis = DateTime.Now;
BarcodeReader barcodeReader = null;
Expand Down
11 changes: 11 additions & 0 deletions src/ZXing.Net.Mobile/Common/MobileBarcodeScanningOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public MobileBarcodeScanningOptions ()
public int DelayBetweenAnalyzingFrames { get;set;}
public int InitialDelayBeforeAnalyzingFrames { get;set; }

/// <summary>
/// If a function is provided here, it is used to customize the preview
/// size of the camera. The function can return null to use the default.
/// </summary>
/// <remarks>
/// Selecting a lower preview size improves performance on some devices.
/// E.g., a 2013 Nexus 7 defaults to 1920 x 1080, the processing of which
/// seems to cause significant lag.
/// </remarks>
public Func<IList<Dimension>, Dimension> PreviewSizeSelector { get;set; }

public static MobileBarcodeScanningOptions Default
{
get { return new MobileBarcodeScanningOptions(); }
Expand Down