How to change rtsp streaming size in c#? - c#

I am using rtsp streaming in c# using below code
However, the code below plays back the original size of the video.
I want to resize the video
How can I increase the size of my video?
video = new VideoCapture();
video.Open(rtspUrl);
using (Mat image = new Mat())
{
while (true)
{
if (!video.Read(image))
{
Cv2.WaitKey();
}
if (!image.Empty())
{
Bitmap bmp = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
pbStream.Image = bmp;
}
if (Cv2.WaitKey(1) >= 0)
break;
if (isRun == false)
{
break;
}
}
}
video = null;

Related

Accord.Video.FFMPEG getting error for long video

I use Accord.Video.FFMPEG to extract images every 10 frames from a video. Total frames for this video is 38194 frames. First run is good, I can save image every 10 frames but after run of about 38185 frames i got null return from this code Bitmap bmpBaseOriginal = vReader.ReadVideoFrame();, if I see in the video there is no problem at the end of video.
I do something like this
using (var vReader = new VideoFileReader())
{
vReader.Open(files[0]);
TotalFrame = vReader.FrameCount;
countin = Convert.ToInt32(TotalFrame / Convert.ToDouble(countAsset));
Fps = vReader.FrameRate.Value;
int a = 0;
for (int i = 0; i < vReader.FrameCount; i++)
{
if(i < vReader.FrameCount - 1)
{
Bitmap bmpBaseOriginal = vReader.ReadVideoFrame();
if (i%10 == 0)
{
a++;
bmpBaseOriginal.Save(string.Format("{0}\\{1}.jpeg", dirVideo.FullName, a), ImageFormat.Jpeg);
}
bmpBaseOriginal.Dispose();
}
else
{
a++;
Bitmap bmpBaseOriginal = vReader.ReadVideoFrame();
bmpBaseOriginal.Save(string.Format("{0}\\{1}.jpeg", dirVideo.FullName, a), ImageFormat.Jpeg);
bmpBaseOriginal.Dispose();
}
}
vReader.Close();
}
this problem occurs again on another video if the video has a lot of frames, but no problem if the video has a less frames.
How to solve it?

ARCore for Unity save camera image

Using ARCore for Unity, trying to save Frame.CameraImage.AcquireCameraImageBytes() as image and scanning the image for QR code. But the converted image is not in actual scale and it is repeating, so not able to deduct the QR code correctly.
Here is my code
void Update()
{
using (var image = Frame.CameraImage.AcquireCameraImageBytes())
{
if (image.IsAvailable)
{
byte[] m_EdgeImage = null;
Color32[] pixels = null;
IParser Parser = new ZXingParser();
if (_texture == null || m_EdgeImage == null || _texture.width != image.Width || _texture.height != image.Height)
{
_texture = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false, false);
m_EdgeImage = new byte[image.Width * image.Height*4];
}
System.Runtime.InteropServices.Marshal.Copy(image.Y, m_EdgeImage, 0, image.Width * image.Height);
_texture.LoadRawTextureData(m_EdgeImage);
_texture.Apply();
ParserResult Result = Parser.Decode(pixels, _texture.width, _texture.height);
if (Result != null)
{
Debug.Log("QRCODE");
}
else
{
var encodedJpg = _texture.EncodeToJPG();
var path = Application.persistentDataPath;
File.WriteAllBytes(path + "/test.jpg", encodedJpg);
Debug.Log("NOQRCODE");
Application.Quit();
}
}
}
}
Here is the converted image
What is wrong here
An ARCore camera image with its data accessible from the CPU in YUV-420-888 formatCheck this. The buffer size is width*height*1.5 for YUV. You may need to convert YUV to RGB format.

Change the photo size Windows Phone 8

In my WP8 app, the user could take pictures and store in my sqlce database. The problem is the size of the pictures that needs to be send by internet and the length of the size is at least 4mb by picture!
I need to reduce the size of the pictures after they are captured in my app.
Here is my code:
private void camera_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK && e.Error == null)
{
image = new System.Windows.Media.Imaging.BitmapImage();
image.SetSource(e.ChosenPhoto);
Imagem.Source = image;
}
}
To get the image to save the image to my sqlce database, I'm using this code:
byte[] ConvertImage()
{
if (image == null) return null;
MemoryStream ms = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(image);
wb.SaveJpeg(ms, image.PixelWidth, image.PixelHeight, 0, 100);
return ms.ToArray();
}
Even save to Jpeg format, the size in bytes are still too big in my opinion.
You have two ways to adjust the size of the image. Resize the image resolution to the smaller one:
WritableBitmapEx sample code:
// Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation method
var resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear)
and/or adjust(decrease) the quality (affect image size):
public static void SaveJpeg(
this WriteableBitmap bitmap,
Stream targetStream,
int targetWidth,
int targetHeight,
int orientation,
int quality
)
You can use both methods.
try this to your code
byte[] ConvertImage()
{
if (image == null) return null;
MemoryStream ms = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(image);
wb.SaveJpeg(ms, image.PixelWidth/4, image.PixelHeight/4, 0, 100);
return ms.ToArray();
}
you can also do /2 or whatever you required

Reset Image Orientation EXIF metadata flag

I am importing a picture into my application and rotating the image according to the EXIF metadata. After this I am saving the rotated image to my disk, but as I have still left the rotated image metadata on the image and windows thinks it should rotate it again... basically meaning my image ends up upside down.
So far I have got:
using (Stream sourceStream = File.Open(dlg.FileName, FileMode.Open, FileAccess.Read))
{
BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.OnLoad);
// Check source is has valid frames
if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
{
sourceDecoder.Frames[0].Metadata.Freeze();
// Get a clone copy of the metadata
BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata.Clone() as BitmapMetadata;
ImportedPhotoMetaData = sourceMetadata;
}
}
and
using (var image = Image.FromFile(dlg.FileName))
{
foreach (var prop in image.PropertyItems)
{
if (prop.Id == 0x112)
{
if (prop.Value[0] == 6)
rotate = 90;
if (prop.Value[0] == 8)
rotate = -90;
if (prop.Value[0] == 3)
rotate = 180;
prop.Value[0] = 1;
}
}
}
but the prop.Value[0] = 1; line does not seem to reset the image metadata. I need to reset the image orientation on the ImportedPhotoMetaData property
Got it...
Replace
prop.Value[0] = 1;
with
ImportedPhotoMetaData.SetQuery("System.Photo.Orientation", 1);

Recording video in C# and WPF from mediaElement

My application applies custom HLSL shader effects to a mediaElement using decorators. How can I record and save the modified video using the application itself in realtime?
I have been using the RenderTargetBitmap object to render image sequences of animations like this:
First you call:
myStoryboard.CurrentTimeInvalidated += new EventHandler(onCurrentTimeInvalidated );
where myStoryboard is the Storyboard driving the animation and then you have the following method:
void onCurrentTimeInvalidated (object sender, EventArgs e)
{
prefix = "";
if (counter < 10)
{
prefix = "000";
}
else if (counter < 100)
{
prefix = "00";
}
else if (counter < 1000)
{
prefix = "0";
}
Size size = new Size(MainCanvas.ActualWidth, MainCanvas.ActualHeight);
MainCanvas.Measure(size);
MainCanvas.Arrange(new Rect(size));
RenderTargetBitmap bmp = new RenderTargetBitmap(imgWidth, imgHeight, 96d, 96d, PixelFormats.Default);
bmp.Render(MainCanvas);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 90;
encoder.Frames.Add(BitmapFrame.Create(bmp));
string file = basePath + prefix + counter.ToString() + "_testpic.jpg";
using (Stream stm = File.Create(file))
{
encoder.Save(stm);
}
counter++;
}
I am not sure how well this will work with the MediaElement but it may be worth a try. For this to work on the MediaElement though you need to drive the MediaElement from a MediaTimeline and call the onCurrentTimeInvalidated method from it's CurrentTimeInvalidated event.

Categories

Resources