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

Manage camera buffer lifetime and limit preview listeners #1024

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ obj
[Oo]bj
[Dd]ebug*/
[Rr]elease*/
_ReSharper.Caches/

*.unsuccessfulbuild
*.pidb
Expand Down
9 changes: 9 additions & 0 deletions Sample.NetAndroid/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thetasoft.android_net_sample" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<application android:allowBackup="true" android:icon="@mipmap/launcher" android:label="ZXing.Net.Mobile" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk android:minSdkVersion="34" android:targetSdkVersion="34" />
</manifest>
83 changes: 83 additions & 0 deletions Sample.NetAndroid/FragmentActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using ZXing.Mobile;
using Android.OS;
using Android.App;
using Android.Widget;
using Android.Content.PM;

namespace Sample.NetAndroid;

[Activity(Label = "ZXing.Net.Mobile", Theme = "@style/Theme.AppCompat.Light", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden)]
public class FragmentActivity : AndroidX.Fragment.App.FragmentActivity
{
ZXingScannerFragment scanFragment;

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

SetContentView(Resource.Layout.FragmentActivity);
}

protected override void OnResume()
{
base.OnResume();


if (scanFragment == null)
{
scanFragment = new ZXingScannerFragment();

SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.fragment_container, scanFragment)
.Commit();
}

Scan();
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) =>
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

protected override void OnPause()
{
scanFragment?.StopScanning();

base.OnPause();
}

void Scan()
{
var opts = new MobileBarcodeScanningOptions
{
PossibleFormats = new List<ZXing.BarcodeFormat> { ZXing.BarcodeFormat.All_1D },
ScanningArea = ScanningArea.From(0f, 0.49f, 1f, 0.51f),
CameraResolutionSelector = availableResolutions =>
{
foreach (var ar in availableResolutions)
{
Console.WriteLine("Resolution: " + ar.Width + "x" + ar.Height);
}

return null;
},
AutoRotate = true
};

scanFragment.StartScanning(
result =>
{
// Null result means scanning was cancelled
if (result == null || string.IsNullOrEmpty(result.Text))
{
Toast.MakeText(this, "Scanning Cancelled", ToastLength.Long).Show();
return;
}

// Otherwise, proceed with result
RunOnUiThread(() => Toast.MakeText(this, "Scanned: " + result.Text, ToastLength.Short).Show());
},
opts);
}
}
22 changes: 22 additions & 0 deletions Sample.NetAndroid/ImageActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Sample.NetAndroid;

[Activity(Label = "ImageActivity")]
public class ImageActivity : Activity
{
ImageView imageBarcode;

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.ImageActivity);

imageBarcode = FindViewById<ImageView>(Resource.Id.imageBarcode);

var barcodeWriter =
new ZXing.Mobile.BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = new ZXing.Common.EncodingOptions { Width = 300, Height = 300 } };
var barcode = barcodeWriter.Write("ZXing.Net.Mobile");

imageBarcode.SetImageBitmap(barcode);
}
}
114 changes: 114 additions & 0 deletions Sample.NetAndroid/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Android.Content.PM;
using Android.Views;
using AndroidX.Fragment.App;
using Xamarin.Essentials;
using ZXing.Mobile;

namespace Sample.NetAndroid
{
[Activity(
Label = "@string/app_name",
MainLauncher = true,
Theme = "@style/Theme.AppCompat.Light",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden)]
public class MainActivity : Activity
{
MobileBarcodeScanner scanner;
Button buttonScanCustomView;
Button buttonScanDefaultView;
Button buttonContinuousScan;
Button buttonFragmentScanner;
Button buttonGenerate;

protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

Platform.Init(Application);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

//Create a new instance of our Scanner
scanner = new MobileBarcodeScanner();

buttonScanDefaultView = FindViewById<Button>(Resource.Id.buttonScanDefaultView);
buttonScanDefaultView.Click +=
async delegate
{
//Tell our scanner to use the default overlay
scanner.UseCustomOverlay = false;

//We can customize the top and bottom text of the default overlay
scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
scanner.BottomText = "Wait for the barcode to automatically scan!";

//Start scanning
var result = await scanner.Scan();

HandleScanResult(result);
};

buttonContinuousScan = FindViewById<Button>(Resource.Id.buttonScanContinuous);
buttonContinuousScan.Click +=
delegate
{
scanner.UseCustomOverlay = false;

//We can customize the top and bottom text of the default overlay
scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
scanner.BottomText = "Wait for the barcode to automatically scan!";

var opt = new MobileBarcodeScanningOptions();
opt.DelayBetweenContinuousScans = 3000;

//Start scanning
scanner.ScanContinuously(opt, HandleScanResult);
};

Button flashButton;
View zxingOverlay;

buttonScanCustomView = this.FindViewById<Button>(Resource.Id.buttonScanCustomView);
buttonScanCustomView.Click +=
async delegate
{
//Tell our scanner we want to use a custom overlay instead of the default
scanner.UseCustomOverlay = true;

//Inflate our custom overlay from a resource layout
zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.ZxingOverlay, null);

//Find the button from our resource layout and wire up the click event
flashButton = zxingOverlay.FindViewById<Button>(Resource.Id.buttonZxingFlash);
flashButton.Click += (sender, e) => scanner.ToggleTorch();

//Set our custom overlay
scanner.CustomOverlay = zxingOverlay;

//Start scanning!
var result = await scanner.Scan(new MobileBarcodeScanningOptions { AutoRotate = true });

HandleScanResult(result);
};

buttonFragmentScanner = FindViewById<Button>(Resource.Id.buttonFragment);
buttonFragmentScanner.Click += delegate { StartActivity(typeof(FragmentActivity)); };

buttonGenerate = FindViewById<Button>(Resource.Id.buttonGenerate);
buttonGenerate.Click += delegate { StartActivity(typeof(ImageActivity)); };
}

void HandleScanResult(ZXing.Result result)
{
var msg = "";

if (result != null && !string.IsNullOrEmpty(result.Text))
msg = "Found Barcode: " + result.Text;
else
msg = "Scanning Canceled!";

RunOnUiThread(() => Toast.MakeText(this, msg, ToastLength.Short).Show());
}
}
}
44 changes: 44 additions & 0 deletions Sample.NetAndroid/Resources/AboutResources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.xml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable/
icon.png

layout/
main.xml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "Resource"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the Resource class would expose:

public class Resource {
public class Drawable {
public const int icon = 0x123;
}

public class Layout {
public const int main = 0x456;
}

public class Strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or
Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string
to reference the first string in the dictionary file values/strings.xml.
25 changes: 25 additions & 0 deletions Sample.NetAndroid/Resources/layout/FragmentActivity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Top View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:text="Bottom View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:gravity="center" />
</LinearLayout>
13 changes: 13 additions & 0 deletions Sample.NetAndroid/Resources/layout/ImageActivity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageBarcode" />
</LinearLayout>
68 changes: 68 additions & 0 deletions Sample.NetAndroid/Resources/layout/ZxingOverlay.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="@android:color/transparent"
android:weightSum="1.0">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_weight="0.3">
<TextView
android:text="Place a barcode in the camera viewfinder to scan it. Barcode will scan Automatically."
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/textView1"
android:gravity="center"
android:layout_weight="0"
android:layout_marginRight="20dp"
android:textSize="12sp"
android:layout_marginLeft="5dp"
android:background="@android:color/transparent"
android:textColor="#ffffffff" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_weight="0.5" />
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:layout_weight="0.2"
android:layout_gravity="center">
<TextView
android:text="Try to avoid shadows and glare. Hold the device back about 6 inches from the barcode."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_gravity="center"
android:textColor="#ffffffff"
android:textSize="12sp"
android:layout_weight="1"
android:gravity="center" />
<Button
android:text="Flash On"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonZxingFlash"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp" />
</LinearLayout>
</LinearLayout>
Loading