|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|