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.6 KiB
47 lines
1.6 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)
|
|
{
|
|
//byte[] rotatedData = new byte[bytes.Length];
|
|
//for (int y = 0; y < height; y++)
|
|
//{
|
|
// for (int x = 0; x < width; x++)
|
|
// {
|
|
// rotatedData[x * height + height - y - 1] = bytes[x + y * width];
|
|
// }
|
|
//}
|
|
var jpegBytes = this.ConvertYuvToJpeg(data);
|
|
var droidBitmap = Android.Graphics.BitmapFactory.DecodeByteArray(jpegBytes, 0, jpegBytes.Length);
|
|
var skBitmap = droidBitmap.ToSKBitmap();
|
|
var base64 = Convert.ToBase64String(jpegBytes);
|
|
}
|
|
|
|
private byte[] ConvertYuvToJpeg(byte[] yuvData, int quality = 80)
|
|
{
|
|
var yuv = new Android.Graphics.YuvImage(yuvData, previewFormat, width, height, null);
|
|
var ms = new MemoryStream();
|
|
yuv.CompressToJpeg(new Android.Graphics.Rect(0, 0, width, height), quality, ms);
|
|
var jpegData = ms.ToArray();
|
|
ms.Close();
|
|
return jpegData;
|
|
}
|
|
}
|
|
} |