Skip to content

Commit

Permalink
migrate nyris searcher to new camera sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
smellouk committed Dec 21, 2024
1 parent a3043ee commit 67ceeb9
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 78 deletions.
57 changes: 57 additions & 0 deletions SDK.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nyris.demo.Android", "demos
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nyris.demo.iOS", "demos\nyris.demo.iOS\nyris.demo.iOS.csproj", "{58A66881-7CAD-45A8-89E8-FFC4B21DD394}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nyris.ui.Android.Camera.Core", "sdk\ui\nyris.ui.Android.Camera.Core\nyris.ui.Android.Camera.Core.csproj", "{27C93873-C31A-4075-A4DB-64A9911AB8E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nyris.ui.Android.Camera.Barcode", "sdk\ui\nyris.ui.Android.Camera.Barcode\nyris.ui.Android.Camera.Barcode.csproj", "{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nyris.ui.Android.Camera.Capture", "sdk\ui\nyris.ui.Android.Camera.Capture\nyris.ui.Android.Camera.Capture.csproj", "{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Expand Down Expand Up @@ -251,6 +257,54 @@ Global
{58A66881-7CAD-45A8-89E8-FFC4B21DD394}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{58A66881-7CAD-45A8-89E8-FFC4B21DD394}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{58A66881-7CAD-45A8-89E8-FFC4B21DD394}.AppStore|iPhone.Build.0 = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Debug|x86.ActiveCfg = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Debug|x86.Build.0 = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Release|x86.ActiveCfg = Release|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Release|x86.Build.0 = Release|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Debug|iPhone.Build.0 = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Release|iPhone.ActiveCfg = Release|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Release|iPhone.Build.0 = Release|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{27C93873-C31A-4075-A4DB-64A9911AB8E5}.AppStore|iPhone.Build.0 = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Debug|x86.ActiveCfg = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Debug|x86.Build.0 = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Release|x86.ActiveCfg = Release|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Release|x86.Build.0 = Release|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Debug|iPhone.Build.0 = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Release|iPhone.ActiveCfg = Release|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Release|iPhone.Build.0 = Release|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E}.AppStore|iPhone.Build.0 = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Debug|x86.ActiveCfg = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Debug|x86.Build.0 = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Release|x86.ActiveCfg = Release|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Release|x86.Build.0 = Release|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Debug|iPhone.Build.0 = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Release|iPhone.ActiveCfg = Release|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Release|iPhone.Build.0 = Release|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA}.AppStore|iPhone.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B1F1804D-1CA4-4B37-BCD8-6696BC0166B8} = {4E829DE1-F2A8-4766-A2C2-236542292155}
Expand All @@ -266,5 +320,8 @@ Global
{BCA3D0F5-F485-4F6F-B225-E2631CD44783} = {A693035B-B34C-4161-9504-ACAEC93525F1}
{C003B4EE-3211-4F48-8E60-27C6B1F8EA58} = {A693035B-B34C-4161-9504-ACAEC93525F1}
{58A66881-7CAD-45A8-89E8-FFC4B21DD394} = {A693035B-B34C-4161-9504-ACAEC93525F1}
{27C93873-C31A-4075-A4DB-64A9911AB8E5} = {FFE36BB3-59C3-44FE-B8AF-D002A427148D}
{F041D6AA-5823-4ABD-A72C-4E6DFF2A710E} = {FFE36BB3-59C3-44FE-B8AF-D002A427148D}
{AA7BA1C9-2414-4642-95C5-C86E3B886CBA} = {FFE36BB3-59C3-44FE-B8AF-D002A427148D}
EndGlobalSection
EndGlobal
5 changes: 4 additions & 1 deletion demos/nyris.demo.MAUI/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:allowBackup="true"
android:icon="@mipmap/appicon"
android:roundIcon="@mipmap/appicon_round"
android:supportsRtl="true"/>
</manifest>
2 changes: 1 addition & 1 deletion sdk/ui/nyris.ui.Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.1" package="nyris.ui.android">
<manifest package="nyris.ui.android">
</manifest>
28 changes: 28 additions & 0 deletions sdk/ui/nyris.ui.Android/Custom/CaptureBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Kotlin.Jvm.Functions;
using Object = Java.Lang.Object;

namespace Nyris.UI.Android.Custom;

public class CaptureBlock<T> : Object, IFunction1 where T : Object
{
private readonly Action<T> OnInvoked;

public CaptureBlock(Action<T> onInvoked)
{
OnInvoked = onInvoked;
}

public Object Invoke(Object? objParameter)
{
try
{
T parameter = (T)objParameter;
OnInvoked.Invoke(parameter);
return null;
}
catch (Exception ex)
{
return null;
}
}
}
75 changes: 75 additions & 0 deletions sdk/ui/nyris.ui.Android/Custom/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Android.Content;
using Android.Graphics;

namespace Nyris.UI.Android.Custom
{
public static class Utils
{
public static byte[] Optimize(this Bitmap bitmap)
{
var proportionalSize = CalculateProportionalSize(bitmap.Width, bitmap.Height);
return Bitmap.CreateScaledBitmap(
bitmap, proportionalSize.Item1, proportionalSize.Item2, true
).ToByteArray();
}

public static Bitmap ToBitmap(this byte[] image)
{
return BitmapFactory.DecodeByteArray(image, 0, image.Length);
}

public static Tuple<int, int> CalculateProportionalSize(int width, int height)
{
const int maxImageSize = 1024;
if (width < maxImageSize && height < maxImageSize)
{
return new Tuple<int, int>(width, height);
}

var aspectRationWidth = maxImageSize / (float) width;
var aspectRationHeight = maxImageSize / (float) height;
var aspectRation = aspectRationWidth > aspectRationHeight ? aspectRationHeight : aspectRationWidth;
return new Tuple<int, int>((int)(width * aspectRation), (int)(height * aspectRation));
}

public static byte[] ToByteArray(this Bitmap bitmap)
{
using (var stream = new MemoryStream())
{
// Compress the bitmap into the stream using the provided format and quality
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);

// Return the byte array from the stream
return stream.ToArray();
}
}

public static String DefaultPathForLastTakenImage(Context context)
{
var directory = new Java.IO.File(context.FilesDir, "searcher");
var file = new Java.IO.File(directory, $"last.jpg");
return file.AbsolutePath;
}

public static void SaveImageToInternalStorage(this byte[] image, Context context)
{
try
{
// Create or open a directory inside the app's internal storage
var directory = new Java.IO.File(context.FilesDir, "searcher");
if (!directory.Exists())
{
directory.Mkdirs(); // Create the directory if it doesn't exist
}

// Write the byte array to the file
var file = new Java.IO.File(directory, $"last.jpg");
File.WriteAllBytes(file.AbsolutePath, image);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving image: {ex.Message}");
}
}
}
}
7 changes: 4 additions & 3 deletions sdk/ui/nyris.ui.Android/Mvp/SearcherContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Android.Graphics;
using AndroidX.Annotations;
using IO.Nyris.Camera;
using Nyris.UI.Android.Models;
using Nyris.UI.Common;

Expand All @@ -19,15 +18,17 @@ internal interface IPresenter : IMvpPresenter<IView>
void OnBackPressed();
void OnOkErrorClick();
void OnPermissionsDenied(IList<string> permissions);

void OnPictureTakenOriginal(byte[] image);
}

internal interface IView : IMvpView<IPresenter>
{
void TintViews(AndroidThemeConfig theme);
void StartCircleViewAnimation();
void SetCaptureLabel(string label);
void AddCameraCallback([NonNull] ICallback callback);
void RemoveCameraCallback([NonNull] ICallback callback);
//void AddCameraCallback([NonNull] ICallback callback);
//void RemoveCameraCallback([NonNull] ICallback callback);
void StartCamera();
void StopCamera();
void HideLabelCapture();
Expand Down
2 changes: 1 addition & 1 deletion sdk/ui/nyris.ui.Android/NyrisSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public INyrisSearcher CameraPermissionDeniedErrorMessage([NonNull] string messag
return this;
}

[Obsolete("This method is deprecated. It's not required anymore and it will be removed in a future version.")]
public INyrisSearcher ExternalStoragePermissionDeniedErrorMessage([NonNull] string message)
{
_config.ExternalStoragePermissionDeniedErrorMessage = message;
return this;
}

Expand Down
46 changes: 17 additions & 29 deletions sdk/ui/nyris.ui.Android/NyrisSearcherActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
using Android.Views;
using AndroidX.AppCompat.App;
using AndroidX.Core.Content;
using IO.Nyris.Camera;
using IO.Nyris.Sdk.Camera;
using IO.Nyris.Croppingview;
using Java.Interop;
using Kotlin.Jvm.Internal;
using Newtonsoft.Json;
using Nyris.UI.Android.Extensions;
using Nyris.UI.Android.Custom;
Expand Down Expand Up @@ -97,10 +98,8 @@ protected override void OnResume()
{
base.OnResume();
var isCameraPermissionGrantted = ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == Permission.Granted;
var isReadSdCardPermissionGrantted = ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted;
var isWriteSdCardPermissionGrantted = ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == Permission.Granted;

if (isCameraPermissionGrantted && isReadSdCardPermissionGrantted && isWriteSdCardPermissionGrantted)
if (isCameraPermissionGrantted)
{
_presenter?.OnResume();
}
Expand All @@ -111,16 +110,6 @@ protected override void OnResume()
{
permissions.Add(Manifest.Permission.Camera);
}
if (!isReadSdCardPermissionGrantted)
{
permissions.Add(Manifest.Permission.ReadExternalStorage);

}
if (!isWriteSdCardPermissionGrantted)
{
permissions.Add(Manifest.Permission.WriteExternalStorage);

}
_presenter.OnPermissionsDenied(permissions);
}
}
Expand Down Expand Up @@ -179,16 +168,6 @@ public void SetCaptureLabel(string label)
_captureLabel.Text = label;
}

public void AddCameraCallback(ICallback callback)
{
_cameraView?.AddCallback(callback);
}

public void RemoveCameraCallback(ICallback callback)
{
_cameraView?.RemoveCallback(callback);
}

public void StartCamera()
{
_cameraView?.Start();
Expand All @@ -197,6 +176,7 @@ public void StartCamera()
public void StopCamera()
{
_cameraView?.Stop();
_cameraView?.Release();
}

public void HideLabelCapture()
Expand Down Expand Up @@ -248,10 +228,18 @@ public void HideLoading()
{
_progress.Hide();
}

public void TakePicture()
{
_cameraView?.TakePicture();
var block = new CaptureBlock<ImageResult>((result) =>
{
result.GetOriginalImage().SaveImageToInternalStorage(this);
RunOnUiThread(() =>
{
_presenter?.OnPictureTakenOriginal(result.GetOriginalImage());
});
});
var kClass = Reflection.GetOrCreateKotlinClass(Java.Lang.Class.FromType(typeof(ImageResult)));
_cameraView?.Capture(0, kClass, block);
}

public void SetImPreviewBitmap(Bitmap bitmap)
Expand Down Expand Up @@ -308,12 +296,12 @@ public void Close()

private void CreateSearcherConfig()
{
var takenImageUri = ImageUtils.Companion.GetPhotoFileUri(this, "photo.jpg");
var takenImageUri = Utils.DefaultPathForLastTakenImage(this);
_settings = GetSharedPreferences("NyrisSearcherSettings", FileCreationMode.Private);

var extraJson = Intent.GetStringExtra(NyrisSearcher.ConfigKey);
_config = JsonConvert.DeserializeObject<NyrisSearcherConfig>(extraJson);
_config.LastTakenPicturePath = takenImageUri.Path;
_config.LastTakenPicturePath = takenImageUri;
_config.LastCroppingRegion = new Common.Region
{
Left = _settings.GetFloat("left", 0),
Expand Down
Loading

0 comments on commit 67ceeb9

Please sign in to comment.