Skip to content

Commit ba1a950

Browse files
author
Viktor Zaika
committed
First changes
1 parent c45ba4f commit ba1a950

115 files changed

Lines changed: 34948 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Autosave files
2+
*~
3+
4+
# build
5+
[Oo]bj/
6+
[Bb]in/
7+
packages/
8+
TestResults/
9+
10+
# globs
11+
Makefile.in
12+
*.DS_Store
13+
*.sln.cache
14+
*.suo
15+
*.cache
16+
*.pidb
17+
*.userprefs
18+
*.usertasks
19+
config.log
20+
config.make
21+
config.status
22+
aclocal.m4
23+
install-sh
24+
autom4te.cache/
25+
*.user
26+
*.tar.gz
27+
tarballs/
28+
test-results/
29+
Thumbs.db
30+
31+
# Mac bundle stuff
32+
*.dmg
33+
*.app
34+
35+
# resharper
36+
*_Resharper.*
37+
*.Resharper
38+
39+
# dotCover
40+
*.dotCover
41+
42+
\.vs/
43+

Acx.Forms.Barcode.Droid/Acx.Forms.Barcode.Droid.csproj

Lines changed: 297 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
7+
<ProjectGuid>{F5DC210F-379A-44CA-8FEA-0F4586240525}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<RootNamespace>Acx.Forms.Barcode.Droid</RootNamespace>
10+
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
11+
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
12+
<AndroidResgenClass>Resource</AndroidResgenClass>
13+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
14+
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
15+
<AssemblyName>Acx.Forms.Barcode.Droid</AssemblyName>
16+
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
17+
<ReleaseVersion>0.7.1</ReleaseVersion>
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug</OutputPath>
24+
<DefineConstants>DEBUG;</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
<AndroidLinkMode>None</AndroidLinkMode>
28+
<ConsolePause>false</ConsolePause>
29+
</PropertyGroup>
30+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release</OutputPath>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
36+
<ConsolePause>false</ConsolePause>
37+
</PropertyGroup>
38+
<ItemGroup>
39+
<Reference Include="System" />
40+
<Reference Include="System.Xml" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="Mono.Android" />
43+
<Reference Include="Xamarin.Android.Support.v4">
44+
<HintPath>..\packages\Xamarin.Android.Support.v4.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v4.dll</HintPath>
45+
</Reference>
46+
<Reference Include="Xamarin.Android.Support.Vector.Drawable">
47+
<HintPath>..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.Vector.Drawable.dll</HintPath>
48+
</Reference>
49+
<Reference Include="Xamarin.Android.Support.Animated.Vector.Drawable">
50+
<HintPath>..\packages\Xamarin.Android.Support.Animated.Vector.Drawable.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.Animated.Vector.Drawable.dll</HintPath>
51+
</Reference>
52+
<Reference Include="Xamarin.Android.Support.v7.AppCompat">
53+
<HintPath>..\packages\Xamarin.Android.Support.v7.AppCompat.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.AppCompat.dll</HintPath>
54+
</Reference>
55+
<Reference Include="Xamarin.Android.Support.v7.RecyclerView">
56+
<HintPath>..\packages\Xamarin.Android.Support.v7.RecyclerView.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.RecyclerView.dll</HintPath>
57+
</Reference>
58+
<Reference Include="Xamarin.Android.Support.Design">
59+
<HintPath>..\packages\Xamarin.Android.Support.Design.23.3.0\lib\MonoAndroid43\Xamarin.Android.Support.Design.dll</HintPath>
60+
</Reference>
61+
<Reference Include="Xamarin.Android.Support.v7.CardView">
62+
<HintPath>..\packages\Xamarin.Android.Support.v7.CardView.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.CardView.dll</HintPath>
63+
</Reference>
64+
<Reference Include="Xamarin.Android.Support.v7.MediaRouter">
65+
<HintPath>..\packages\Xamarin.Android.Support.v7.MediaRouter.23.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.MediaRouter.dll</HintPath>
66+
</Reference>
67+
<Reference Include="Xamarin.Forms.Platform.Android">
68+
<HintPath>..\packages\Xamarin.Forms.2.3.0.107\lib\MonoAndroid10\Xamarin.Forms.Platform.Android.dll</HintPath>
69+
</Reference>
70+
<Reference Include="FormsViewGroup">
71+
<HintPath>..\packages\Xamarin.Forms.2.3.0.107\lib\MonoAndroid10\FormsViewGroup.dll</HintPath>
72+
</Reference>
73+
<Reference Include="Xamarin.Forms.Core">
74+
<HintPath>..\packages\Xamarin.Forms.2.3.0.107\lib\MonoAndroid10\Xamarin.Forms.Core.dll</HintPath>
75+
</Reference>
76+
<Reference Include="Xamarin.Forms.Xaml">
77+
<HintPath>..\packages\Xamarin.Forms.2.3.0.107\lib\MonoAndroid10\Xamarin.Forms.Xaml.dll</HintPath>
78+
</Reference>
79+
<Reference Include="Xamarin.Forms.Platform">
80+
<HintPath>..\packages\Xamarin.Forms.2.3.0.107\lib\MonoAndroid10\Xamarin.Forms.Platform.dll</HintPath>
81+
</Reference>
82+
<Reference Include="Xamarin.GooglePlayServices.Basement">
83+
<HintPath>..\packages\Xamarin.GooglePlayServices.Basement.29.0.0.1\lib\MonoAndroid41\Xamarin.GooglePlayServices.Basement.dll</HintPath>
84+
</Reference>
85+
<Reference Include="Xamarin.GooglePlayServices.Base">
86+
<HintPath>..\packages\Xamarin.GooglePlayServices.Base.29.0.0.1\lib\MonoAndroid41\Xamarin.GooglePlayServices.Base.dll</HintPath>
87+
</Reference>
88+
<Reference Include="Xamarin.GooglePlayServices.Vision">
89+
<HintPath>..\packages\Xamarin.GooglePlayServices.Vision.29.0.0.1\lib\MonoAndroid41\Xamarin.GooglePlayServices.Vision.dll</HintPath>
90+
</Reference>
91+
</ItemGroup>
92+
<ItemGroup>
93+
<Compile Include="Resources\Resource.designer.cs" />
94+
<Compile Include="Camera\CameraConfigurator.cs" />
95+
<Compile Include="Logger\Log.cs" />
96+
<Compile Include="BarcodeScannerRenderer.cs" />
97+
<Compile Include="View\AutoFocusCallback.cs" />
98+
<Compile Include="BarcodeTrackerFactory.cs" />
99+
<Compile Include="Configuration.cs" />
100+
<Compile Include="Camera\CameraServiceFactory.cs" />
101+
<Compile Include="Camera\CameraSourceExtensions.cs" />
102+
<Compile Include="BarcodeTracker.cs" />
103+
<Compile Include="Platform.cs" />
104+
<Compile Include="Camera\CameraService.cs" />
105+
</ItemGroup>
106+
<ItemGroup>
107+
<None Include="Resources\AboutResources.txt" />
108+
<None Include="packages.config" />
109+
</ItemGroup>
110+
<ItemGroup>
111+
<AndroidResource Include="Resources\values\Strings.xml" />
112+
</ItemGroup>
113+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
114+
<Import Project="..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets" Condition="Exists('..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets')" />
115+
<Import Project="..\packages\Xamarin.Forms.2.3.0.107\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.2.3.0.107\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
116+
<Import Project="..\packages\Xamarin.GooglePlayServices.Basement.29.0.0.1\build\Xamarin.GooglePlayServices.Basement.targets" Condition="Exists('..\packages\Xamarin.GooglePlayServices.Basement.29.0.0.1\build\Xamarin.GooglePlayServices.Basement.targets')" />
117+
<ItemGroup>
118+
<ProjectReference Include="..\Acx.Forms.Barcode.Pcl\Acx.Forms.Barcode.Pcl.csproj">
119+
<Project>{677C433A-8C1F-466F-91CA-1181FDF29795}</Project>
120+
<Name>Acx.Forms.Barcode.Pcl</Name>
121+
</ProjectReference>
122+
<ProjectReference Include="..\CameraSourceBindings\CameraSourceBindings.csproj">
123+
<Project>{8B5FB435-D2A0-45B7-BD04-F23D33D8AE10}</Project>
124+
<Name>CameraSourceBindings</Name>
125+
</ProjectReference>
126+
</ItemGroup>
127+
<ItemGroup>
128+
<Folder Include="View\" />
129+
</ItemGroup>
130+
</Project>
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Threading.Tasks;
4+
using Android.Content.PM;
5+
using Android.Runtime;
6+
using Android.Views;
7+
using Acx.Forms.Barcode.Droid;
8+
using Acx.Forms.Barcode.Droid.Camera;
9+
using Acx.Forms.Barcode.Droid.Logger;
10+
using Acx.Forms.Barcode.Droid.View;
11+
using Acx.Forms.Barcode.Pcl;
12+
using Acx.Forms.Barcode.Pcl.Logger;
13+
using Xamarin.Forms;
14+
using Xamarin.Forms.Platform.Android;
15+
16+
[assembly: ExportRenderer(typeof(BarcodeScanner), typeof(BarcodeScannerRenderer))]
17+
namespace Acx.Forms.Barcode.Droid
18+
{
19+
public class BarcodeScannerRenderer : ViewRenderer<BarcodeScanner, SurfaceView>, ISurfaceHolderCallback
20+
{
21+
private static Configuration config;
22+
private readonly CameraConfigurator configurator;
23+
private CameraService cameraServiceHolder;
24+
25+
private CameraService CameraService {
26+
get {
27+
if (null == cameraServiceHolder) {
28+
var factory = new CameraServiceFactory ();
29+
cameraServiceHolder = factory.Create (Context, Element, configurator);
30+
}
31+
32+
return cameraServiceHolder;
33+
}
34+
}
35+
36+
private Lazy<Platform> platform;
37+
38+
private Platform Platform {
39+
get {
40+
return platform.Value;
41+
}
42+
}
43+
44+
private bool HasValidSurface {
45+
get {
46+
return Control?.Holder.Surface.IsValid == true;
47+
}
48+
}
49+
50+
public static void Init ()
51+
{
52+
Init (new Configuration ());
53+
}
54+
55+
public static void Init (Configuration config)
56+
{
57+
BarcodeScannerRenderer.config = config;
58+
}
59+
60+
public BarcodeScannerRenderer ()
61+
{
62+
configurator = new CameraConfigurator ().SetConfiguration (config);
63+
platform = new Lazy<Platform> (() => new Platform ());
64+
}
65+
66+
public async void SurfaceCreated (ISurfaceHolder holder)
67+
{
68+
// Nothing to do.
69+
}
70+
71+
public async void SurfaceChanged (ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
72+
{
73+
if (!HasValidSurface) {
74+
return;
75+
}
76+
77+
// portrait mode
78+
if (height > width) {
79+
CameraService.SetViewSize (height, width);
80+
} else {
81+
CameraService.SetViewSize (width, height);
82+
}
83+
84+
if (!Element.IsEnabled) {
85+
return;
86+
}
87+
88+
CameraService.HaltPreview ();
89+
await Task.Run (() => {
90+
CameraService.StartPreview (holder);
91+
});
92+
}
93+
94+
public void SurfaceDestroyed (ISurfaceHolder holder)
95+
{
96+
Element.IsEnabled = false;
97+
}
98+
99+
protected override void OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs<BarcodeScanner> e)
100+
{
101+
base.OnElementChanged (e);
102+
103+
if (Element != null && Element.IsEnabled)
104+
InitializeView();
105+
}
106+
107+
protected async override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
108+
{
109+
base.OnElementPropertyChanged (sender, e);
110+
111+
if (e.PropertyName == Xamarin.Forms.VisualElement.IsEnabledProperty.PropertyName) {
112+
if (Element.IsEnabled) {
113+
InitializeView();
114+
}
115+
116+
if (!Element.IsEnabled) {
117+
ReleaseCamera ();
118+
}
119+
}
120+
121+
if (e.PropertyName == BarcodeScanner.PreviewActiveProperty.PropertyName) {
122+
if (Element.PreviewActive) {
123+
await Task.Run (() => CameraService.StartPreview (Control.Holder));
124+
}
125+
126+
if (!Element.PreviewActive) {
127+
CameraService.HaltPreview ();
128+
}
129+
}
130+
131+
if (e.PropertyName == BarcodeScanner.TorchProperty.PropertyName) {
132+
133+
if (!Platform.HasFlashPermission) {
134+
return;
135+
}
136+
137+
if (!Platform.HasFlash) {
138+
return;
139+
}
140+
141+
CameraService.ToggleTorch (Element.Torch);
142+
}
143+
144+
if (e.PropertyName == BarcodeScanner.BarcodeDecoderProperty.PropertyName) {
145+
if (Element.BarcodeDecoder) {
146+
CameraService.StartDecoder ();
147+
}
148+
149+
if (!Element.BarcodeDecoder) {
150+
CameraService.StopDecoder ();
151+
}
152+
}
153+
}
154+
155+
private void InitializeView()
156+
{
157+
if (!Platform.HasCameraPermission) {
158+
return;
159+
}
160+
161+
if (!Platform.HasCamera) {
162+
return;
163+
}
164+
165+
if (!Platform.IsGmsReady) {
166+
return;
167+
}
168+
169+
if (Control != null) {
170+
ActivatePreview ();
171+
return;
172+
}
173+
174+
var surfaceView = new SurfaceView (Context);
175+
surfaceView.Holder.AddCallback (this);
176+
SetNativeControl (surfaceView);
177+
178+
Element.CameraOpened += async (sender, args) => {
179+
if (Element.PreviewActive) {
180+
await Task.Run (() => {
181+
if (Element.IsEnabled) {
182+
CameraService.StartPreview (Control.Holder);
183+
CameraService.ToggleTorch (Element.Torch);
184+
}
185+
});
186+
}
187+
};
188+
}
189+
190+
private async void ActivatePreview()
191+
{
192+
if (Element.PreviewActive) {
193+
await Task.Run (() => {
194+
if (Element.IsEnabled) {
195+
CameraService.StartPreview (Control.Holder);
196+
CameraService.ToggleTorch (Element.Torch);
197+
}
198+
});
199+
}
200+
}
201+
202+
protected override void Dispose (bool disposing)
203+
{
204+
ReleaseCamera ();
205+
base.Dispose (disposing);
206+
}
207+
208+
private void ReleaseCamera ()
209+
{
210+
CameraService?.ReleaseCamera ();
211+
cameraServiceHolder = null;
212+
}
213+
}
214+
}

0 commit comments

Comments
 (0)