You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
using Android.Hardware;
|
|
using SkiaSharp.Views.Android;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace Demo.Droid.Controls
|
|
{
|
|
public class PreviewCallback : Java.Lang.Object, Camera.IPreviewCallback
|
|
{
|
|
private int width;
|
|
private int height;
|
|
private Android.Graphics.ImageFormatType previewFormat;
|
|
|
|
public PreviewCallback(int width, int height, Android.Graphics.ImageFormatType previewFormat)
|
|
{
|
|
this.width = width;
|
|
this.height = height;
|
|
this.previewFormat = previewFormat;
|
|
}
|
|
|
|
public void OnPreviewFrame(byte[] data, Camera camera)
|
|
{
|
|
var jpegBytes = this.ConvertYuvToJpeg(data);
|
|
using (var droidBitmap = Android.Graphics.BitmapFactory.DecodeByteArray(jpegBytes, 0, jpegBytes.Length))
|
|
{
|
|
using (var skBitmap = droidBitmap.ToSKBitmap())
|
|
{
|
|
var base64 = Convert.ToBase64String(jpegBytes);
|
|
}
|
|
}
|
|
}
|
|
|
|
private byte[] ConvertYuvToJpeg(byte[] yuvData, int quality = 80)
|
|
{
|
|
byte[] result = null;
|
|
using (var yuv = new Android.Graphics.YuvImage(yuvData, previewFormat, width, height, null))
|
|
{
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
yuv.CompressToJpeg(new Android.Graphics.Rect(0, 0, width, height), quality, ms);
|
|
result = ms.ToArray();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
} |