How could I add a text into an irandomaccessstream in UWP?

  • Thread starter Thread starter tuannguyen.dev
  • Start date Start date
T

tuannguyen.dev

Guest
Here is my function record ip camera. How can I insert time into each image (_frameBuffer) that I have. When I finish the record, I get a video containing the image and time that I added to each image.

public async void Record(byte[] _frameBuffer)
{
using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(_frameBuffer.AsBuffer());
stream.Seek(0);

var decoder = await BitmapDecoder.CreateAsync(stream);
PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
new BitmapTransform { ScaledWidth = this.PixelWidth, ScaledHeight = this.PixelHeight },
ExifOrientationMode.RespectExifOrientation,
ColorManagementMode.ColorManageToSRgb);
if (this.PictureWriter != null)
PictureWriter?.AddFrame(dataProvider.DetachPixelData(), (int)this.PixelWidth, (int)this.PixelHeight);
stream.Dispose();
}
}

I am very grateful to receive help from everyone

What I have tried:

I have found a solution that can insert a text into an image. But in my case, I have not found a way to solve it.

private async void Button_Click(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
var sourceFile = await picker.PickSingleFileAsync();

if (sourceFile == null) { return; }

var device = CanvasDevice.GetSharedDevice();
var image = default(CanvasBitmap);
using (var s = await sourceFile.OpenReadAsync())
{
image = await CanvasBitmap.LoadAsync(device, s);
}
var offscreen = new CanvasRenderTarget(
device, (float)image.Bounds.Width, (float)image.Bounds.Height, 96);

using (var ds = offscreen.CreateDrawingSession())
{
ds.DrawImage(image, 0, 0);
ds.DrawText("Hello world", 10, 10, Colors.Blue);
}
var displayInformation = DisplayInformation.GetForCurrentView();
var savepicker = new FileSavePicker();
savepicker.FileTypeChoices.Add("png", new List<string> { ".png" });
var destFile = await savepicker.PickSaveFileAsync();

using (var s = await destFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, s);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)offscreen.Size.Width,
(uint)offscreen.Size.Height,
displayInformation.LogicalDpi,
displayInformation.LogicalDpi,
offscreen.GetPixelBytes());
await encoder.FlushAsync();
}
}

Continue reading...
 
Back
Top