xamarin 获取摄像头byte array

Former-commit-id: 18e5a420aaeb5694df7968156397d52ad8eec826
TangShanKaiPing
wanggang 6 years ago
parent a5cf5e3a21
commit e7edc93a42

@ -13,9 +13,9 @@ namespace Demo.iOS
this.Notification();
LibVLCSharpFormsRenderer.Init();
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
CameraPreview.iOS.CameraPreviewSettings.Instance.Init(new IosDecoder());
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
CameraPreview.iOS.CameraPreviewSettings.Instance.Init(new IosDecoder());
return base.FinishedLaunching(app, options);
}

@ -59,6 +59,7 @@
<CodesignExtraArgs />
<CodesignResourceRules />
<MtouchEnableSGenConc>false</MtouchEnableSGenConc>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
<DebugType>none</DebugType>
@ -162,10 +163,6 @@
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Drawing.dll</HintPath>
</Reference>
<Reference Include="System.Web.Services" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />

@ -1,10 +1,9 @@
using CameraPreview;
using CameraPreview.iOS;
using CoreGraphics;
using CoreVideo;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ZXing.Mobile;
using UIKit;
namespace Demo.iOS
{
@ -14,47 +13,31 @@ namespace Demo.iOS
public override IScanResult Decode(CVPixelBuffer pixelBuffer)
{
unsafe
pixelBuffer.Lock(CVPixelBufferLock.None);
var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
using (var cs = CGColorSpace.CreateDeviceRGB())
{
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);
using (Bitmap image = new Bitmap(luminanceSource.Width, luminanceSource.Height))
using (var context = new CGBitmapContext(pixelBuffer.BaseAddress,
pixelBuffer.Width,
pixelBuffer.Height,
8,
pixelBuffer.BytesPerRow,
cs,
(CGImageAlphaInfo)flags))
{
for (int r = 0; r < height; r++)
using (var cgImage = context.ToImage())
{
for (int c = 0; c < width; c++)
pixelBuffer.Unlock(CVPixelBufferLock.None);
var uiImage = UIImage.FromImage(cgImage);
using (var stream = new MemoryStream())
{
var value = luminanceSource.Matrix[r * width + c];
image.SetPixel(c, r, Color.FromArgb(value, value, value));
uiImage.AsJPEG().AsStream().CopyTo(stream);
this._faceDecoder.Decode(stream.ToArray());
}
}
using (var stream = new MemoryStream())
{
var @params = new EncoderParameters(1);
@params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
image.Save(stream, GetEncoderInfo("image/jpeg"), @params);
this._faceDecoder.Decode(stream.ToArray());
}
this._faceDecoder.Decode(luminanceSource.Matrix);
}
}
return base.Decode(pixelBuffer);
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
}

@ -6,7 +6,6 @@ namespace Demo
{
public void Decode(byte[] image)
{
//Console.WriteLine(BitConverter.ToString(image).Replace("-", ""));
var base64 = Convert.ToBase64String(image);
//Console.WriteLine();
}

Loading…
Cancel
Save