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

Optimize Code #1

Open
wants to merge 9 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using CameraPreview.Droid;
using CameraPreview.Droid;

namespace ZXing.Net.Droid
{
Expand All @@ -10,4 +9,4 @@ public static void Init()
CameraPreviewSettings.Instance.Init(new ZXingDecoder());
}
}
}
}
14 changes: 8 additions & 6 deletions Plugins/ZXing/ZXing.Net.Droid/ZXing.Net.Droid.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -8,7 +8,7 @@
<OutputType>Library</OutputType>
<RootNamespace>ZXing.Net.Droid</RootNamespace>
<AssemblyName>ZXing.Net.Droid</AssemblyName>
<TargetFrameworkVersion>v8.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -19,7 +19,8 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<JavaMaximumHeapSize></JavaMaximumHeapSize>
<JavaMaximumHeapSize>
</JavaMaximumHeapSize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,7 +31,8 @@
<WarningLevel>4</WarningLevel>
<AndroidManagedSymbols>true</AndroidManagedSymbols>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<JavaMaximumHeapSize></JavaMaximumHeapSize>
<JavaMaximumHeapSize>
</JavaMaximumHeapSize>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -41,7 +43,7 @@
<HintPath>..\..\..\packages\ZXing.Net.0.16.4\lib\MonoAndroid\zxing.monoandroid.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ZXingDecoder.cs" />
<Compile Include="CameraPreviewSettingsForZXing.cs" />
Expand All @@ -63,6 +65,6 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\..\packages\NETStandard.Library.2.0.0\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('..\..\packages\NETStandard.Library.2.0.0\build\netstandard2.0\NETStandard.Library.targets')" />
</Project>
7 changes: 4 additions & 3 deletions Plugins/ZXing/ZXing.Net.Droid/ZXingDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using CameraPreview;
using CameraPreview.Droid;
using ZXing.Net.Xamarin.Forms;
using ZXing;

namespace ZXing.Net.Droid
{
public class ZXingDecoder : DefaultDecoderBase
{
BarcodeReader _reader;
private readonly BarcodeReader _reader;

public ZXingDecoder()
{
_reader = new BarcodeReader();
Expand All @@ -31,6 +31,7 @@ public override IScanResult Decode(Bitmap image)
result.Timestamp = res.Timestamp;
result.Text = res.Text;
}

PerformanceCounter.Stop(decoder, "ZXing Decoder take {0} ms.");
return result;
}
Expand All @@ -56,4 +57,4 @@ public override void ScanningOptionsUpdate(ScanningOptionsBase options)
}
}
}
}
}
6 changes: 2 additions & 4 deletions Plugins/ZXing/ZXing.Net.Xamarin.Forms/ZXingOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using CameraPreview;
using CameraPreview;

namespace ZXing.Net.Xamarin.Forms
{
Expand All @@ -19,4 +17,4 @@ public ZXingOptions() : base()
public bool? AssumeGS1 { get; set; }

}
}
}
41 changes: 20 additions & 21 deletions Plugins/ZXing/ZXing.Net.Xamarin.Forms/ZXingOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms;

namespace ZXing.Net.Xamarin.Forms
{
public class ZXingOverlay : Grid
{
Label topText;
Label botText;
private readonly Label _topText;
private readonly Label _botText;

public ZXingOverlay()
{
Expand All @@ -16,10 +14,10 @@ public ZXingOverlay()
VerticalOptions = LayoutOptions.FillAndExpand;
HorizontalOptions = LayoutOptions.FillAndExpand;

RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition { Height = new GridLength(2, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition {Height = new GridLength(1, GridUnitType.Star)});
RowDefinitions.Add(new RowDefinition {Height = new GridLength(2, GridUnitType.Star)});
RowDefinitions.Add(new RowDefinition {Height = new GridLength(1, GridUnitType.Star)});
ColumnDefinitions.Add(new ColumnDefinition {Width = new GridLength(1, GridUnitType.Star)});


Children.Add(new BoxView
Expand Down Expand Up @@ -47,41 +45,42 @@ public ZXingOverlay()
Opacity = 0.6,
}, 0, 1);

topText = new Label
_topText = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
TextColor = Color.White,
};
topText.SetBinding(Label.TextProperty, new Binding(nameof(TopText)));
Children.Add(topText, 0, 0);
_topText.SetBinding(Label.TextProperty, new Binding(nameof(TopText)));
Children.Add(_topText, 0, 0);

botText = new Label
_botText = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
TextColor = Color.White,
};
botText.SetBinding(Label.TextProperty, new Binding(nameof(BottomText)));
Children.Add(botText, 0, 2);
_botText.SetBinding(Label.TextProperty, new Binding(nameof(BottomText)));
Children.Add(_botText, 0, 2);

}

public static readonly BindableProperty TopTextProperty =
BindableProperty.Create(nameof(TopText), typeof(string), typeof(ZXingOverlay), string.Empty);

public string TopText
{
get { return (string)GetValue(TopTextProperty); }
set { SetValue(TopTextProperty, value); }
get => (string) GetValue(TopTextProperty);
set => SetValue(TopTextProperty, value);
}

public static readonly BindableProperty BottomTextProperty =
BindableProperty.Create(nameof(BottomText), typeof(string), typeof(ZXingOverlay), string.Empty);

public string BottomText
{
get { return (string)GetValue(BottomTextProperty); }
set { SetValue(BottomTextProperty, value); }
get => (string) GetValue(BottomTextProperty);
set => SetValue(BottomTextProperty, value);
}
}
}

}
5 changes: 2 additions & 3 deletions Plugins/ZXing/ZXing.Net.Xamarin.Forms/ZXingResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using CameraPreview;
using CameraPreview;

namespace ZXing.Net.Xamarin.Forms
{
Expand All @@ -11,4 +10,4 @@ public class ZXingResult : IScanResult

public string Text { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
namespace ZXing.Net.iOS
namespace ZXing.Net.iOS
{
public class CVPixelBufferBGRA32LuminanceSource : BaseLuminanceSource
{
public unsafe CVPixelBufferBGRA32LuminanceSource(byte* cvPixelByteArray, int cvPixelByteArrayLength, int width, int height)
public unsafe CVPixelBufferBGRA32LuminanceSource(byte* cvPixelByteArray, int cvPixelByteArrayLength, int width,
int height)
: base(width, height)
{
CalculateLuminance(cvPixelByteArray, cvPixelByteArrayLength);
Expand All @@ -17,15 +17,18 @@ public CVPixelBufferBGRA32LuminanceSource(byte[] luminances, int width, int heig

unsafe void CalculateLuminance(byte* rgbRawBytes, int bytesLen)
{
for (int rgbIndex = 0, luminanceIndex = 0; rgbIndex < bytesLen && luminanceIndex < luminances.Length; luminanceIndex++)
for (int rgbIndex = 0, luminanceIndex = 0;
rgbIndex < bytesLen && luminanceIndex < luminances.Length;
luminanceIndex++)
{
// Calculate luminance cheaply, favoring green.
var b = rgbRawBytes[rgbIndex++];
var g = rgbRawBytes[rgbIndex++];
var r = rgbRawBytes[rgbIndex++];
var alpha = rgbRawBytes[rgbIndex++];
var luminance = (byte)((RChannelWeight * r + GChannelWeight * g + BChannelWeight * b) >> ChannelWeight);
luminances[luminanceIndex] = (byte)(((luminance * alpha) >> 8) + (255 * (255 - alpha) >> 8));
var luminance =
(byte) ((RChannelWeight * r + GChannelWeight * g + BChannelWeight * b) >> ChannelWeight);
luminances[luminanceIndex] = (byte) (((luminance * alpha) >> 8) + (255 * (255 - alpha) >> 8));
}
}

Expand Down
5 changes: 2 additions & 3 deletions Plugins/ZXing/ZXing.Net.iOS/CameraPreviewSettingsForZXing.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using CameraPreview.iOS;
using CameraPreview.iOS;

namespace ZXing.Net.iOS
{
Expand All @@ -10,4 +9,4 @@ public static void Init()
CameraPreviewSettings.Instance.Init(new ZXingDecoder());
}
}
}
}
16 changes: 11 additions & 5 deletions Plugins/ZXing/ZXing.Net.iOS/ZXing.Net.iOS.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -25,22 +25,26 @@
<IOSDebuggerPort>62396</IOSDebuggerPort>
<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>
<DeviceSpecificBuild>false</DeviceSpecificBuild>
<MtouchVerbosity></MtouchVerbosity>
<MtouchLink></MtouchLink>
<MtouchVerbosity>
</MtouchVerbosity>
<MtouchLink>
</MtouchLink>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<DefineConstants></DefineConstants>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchNoSymbolStrip>true</MtouchNoSymbolStrip>
<MtouchLink>SdkOnly</MtouchLink>
<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>
<MtouchVerbosity></MtouchVerbosity>
<MtouchVerbosity>
</MtouchVerbosity>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
Expand Down Expand Up @@ -70,6 +74,8 @@
<ProjectReference Include="..\..\..\Source\CameraPreview.iOS\CameraPreview.iOS.csproj">
<Project>{547CB727-881C-43B7-864E-AEAC7ED5EFDC}</Project>
<Name>CameraPreview.iOS</Name>
<IsAppExtension>false</IsAppExtension>
<IsWatchApp>false</IsWatchApp>
</ProjectReference>
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 7 additions & 5 deletions Plugins/ZXing/ZXing.Net.iOS/ZXingDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace ZXing.Net.iOS
public class ZXingDecoder : DefaultDecoderBase
{
BarcodeReader _reader;

public ZXingDecoder() : base()
{
_reader = new BarcodeReader();
Expand All @@ -19,10 +20,10 @@ public override IScanResult Decode(CVPixelBuffer pixelBuffer)
var decoder = PerformanceCounter.Start();
unsafe
{
var rawData = (byte*)pixelBuffer.BaseAddress.ToPointer();
int rawDatalen = (int)(pixelBuffer.Height * pixelBuffer.Width * 4);
int width = (int)pixelBuffer.Width;
int height = (int)pixelBuffer.Height;
var rawData = (byte*) pixelBuffer.BaseAddress.ToPointer();
int rawDatalen = (int) (pixelBuffer.Height * pixelBuffer.Width * 4);
int width = (int) pixelBuffer.Width;
int height = (int) pixelBuffer.Height;
var luminanceSource = new CVPixelBufferBGRA32LuminanceSource(rawData, rawDatalen, width, height);
var res = _reader.Decode(luminanceSource);
var result = new ZXingResult();
Expand All @@ -37,6 +38,7 @@ public override IScanResult Decode(CVPixelBuffer pixelBuffer)
result.Timestamp = res.Timestamp;
result.Text = res.Text;
}

PerformanceCounter.Stop(decoder, "ZXing Decoder take {0} ms.");
return result;
}
Expand All @@ -63,4 +65,4 @@ public override void ScanningOptionsUpdate(ScanningOptionsBase options)
}
}
}
}
}
Loading