How to prevent the mirror imaging issue on Xamarin UWP - c#

Hi I have a Xamarin application, I need to scan some items(Debit card, Id card) through camera and upload the same to server. Implemented the camera functionality through Xamarin.Essential.Media and it's working on iOS back camera. But in UWP am facing the camera mirroring issue, is there any solution for this. And also how to prevent the iOS front camera. Here is my code.
var photo = await MediaPicker.CapturePhotoAsync();
 if (photo == null)
{ return;}
byte[] imgByte;
using (var bytestream = await photo.OpenReadAsync())
{
MemoryStream ms = new MemoryStream();
bytestream.CopyTo(ms);    
imgByte = ms.ToArray()
}
    
While Capturing
After Taking, how to avoid the mirroring while capturing the image

Related

Capturing digital TV and stream it on local network

I need to capture digital TV signals and stream the TV channels on the local network (via Http, RTSP or any protocol like this). I'm using CodeTV to find TV channels, capture and decode them. The project uses DirectShow.Net to do that. I found Vlc.DotNet helpful to stream data on the local network. The problem is, I'm not familiar with Directshow and I can't figure it out how to get the video stream and give it to the Vlc library.
I tried to replace this code with the code that records the video stream but "bs" remains null.
IStream bs = this.currentGraphBuilder as IStream;
var currentDirectory =
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
currentDirectory = Path.Combine(currentDirectory, "libvlc");
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory,
IntPtr.Size == 4 ? "win-x86" : "win-x64"));
using (Vlc.DotNet.Core.VlcMediaPlayer mediaPlayer = new
Vlc.DotNet.Core.VlcMediaPlayer(libDirectory))
{
var mediaOptions = new[]
{
":sout=#rtp{sdp=rtsp://127.0.0.1:554/}",
":sout-keep"
};
mediaPlayer.SetMedia(bs as Stream, mediaOptions);
mediaPlayer.Play();
}
I don't know if I need to create a filter and add it to the graph or there is a simpler way.

How to get ImageStream from MediaCapture?

In WP 8 I used PhotoCamera to make a camera app and to save the image in camera roll I used this method:
private void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
string fileName = "photo.jpg";
MediaLibrary library = new MediaLibrary();
library.SavePictureToCameraRoll(fileName, e.ImageStream);
}
In WPSL 8.1 I use MediaCapture and I use the same style to save image in camera roll but I don't know how to retrieve ImageStream from MediaCapture like in e.ImageStream. I am open to suggestions even with other programming style for saving to camera roll.
var file = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(IMAGECAPTURE_FILENAME, Windows.Storage.CreationCollisionOption.ReplaceExisting);
await _exceptionHandler.Run(async () =>
{
await _mediaCapture.CapturePhotoToStorageFileAsync(_imageEncodingProperties, file);
var photoStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
await bitmap.SetSourceAsync(photoStream);
});
The above is taken from a UWP app to save an image to storage, then read it from disk as a stream. I've never been able to capture the image directly as a stream.

Take photo to buffer/array Windows universal app c#

Please help me.
I'm trying to get photo from camera to buffer in order to make image processing later.
I found that I should use MediaCapture element to work with camera, but I didn't find how to save frame into buffer.
You can save frame to bitmap and process it later. Here is the OCR sample code that provided by Microsoft on GitHub:
// Create the video frame to request a SoftwareBitmap preview frame.
var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, videoFrameWidth, videoFrameHeight);
// Capture the preview frame.
using (var currentFrame = await mediaCapture.GetPreviewFrameAsync(videoFrame))
{
// Collect the resulting frame.
SoftwareBitmap bitmap = currentFrame.SoftwareBitmap;
...
var ocrResult = await ocrEngine.RecognizeAsync(bitmap);
}
one more example: CaptureElement https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CameraManualControls
Saving the photo:
private async Task TakePhotoAsync()
{
var stream = new InMemoryRandomAccessStream();
try
{
await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
var photoOrientation = ConvertOrientationToPhotoOrientation(GetCameraOrientation());
await ReencodeAndSavePhotoAsync(stream, photoOrientation);
}
catch (Exception ex)
{
}
}

How do i take a photo with the correct rotation, aspect ratio in Windows Phone 8.1? (using MediaCapture)

Can any of you provide an actual working sample of how to take and save a photo using the MediaCapture element. I've tried looking for an actual solution in MSDN but none of those explanations or code actually describe the process in a simple way.
I need to take a picture and save it to my library (i need to show the correct preview for this), however right now it is rotated 90 degrees and i can't adjust it. I've tried setting the rotation of the video preview and it works for the preview however when i do this the aspect ratio its all wrong and the saved image its not correct.
The examples from channel 9 kind of suck too. I just need a simple implementation...
Im using a Runtime app NOT a silverlight app for Windows Phone 8.1.
I have had the same issue, SetRecordRotation doesn't work for me. I found workaround - take photo and rotate an image, it works great. I use method like that:
private async void CapturePhoto()
{
string photoPath = string.Empty;
ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg();
using (var imageStream = new InMemoryRandomAccessStream())
{
await MediaCapture.CapturePhotoToStreamAsync(format, imageStream);
BitmapDecoder dec = await BitmapDecoder.CreateAsync(imageStream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(imageStream, dec);
enc.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
await enc.FlushAsync();
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile capturefile = await folder.CreateFileAsync("photo.jpg", CreationCollisionOption.GenerateUniqueName);
photoPath = capturefile.Name;
using (var fileStream = await capturefile.OpenAsync(FileAccessMode.ReadWrite))
{
try
{
await RandomAccessStream.CopyAsync(imageStream, fileStream);
}
catch {}
}
}
}
I modified sample of code from article How to capture a photo in your Windows Phone 8.1 Runtime app by Marco Siccardi
http://dotnet.dzone.com/articles/how-capture-photo-your-windows-0
There are two samples posted on the Microsoft github page that are relevant, although they target Windows 10. Still, the APIs should work on 8/8.1.
GetPreviewFrame: This sample will not lock the page rotation, and apply a corrective rotation to the preview stream. It does not use SetPreviewRotation, as that method is more resource-heavy than using the metadata approach. This sample doesn't capture photos (just preview frames)
UniversalCameraSample: This one does capture photos, and supports portrait and landscape orientations. Here is the relevant part:
var stream = new InMemoryRandomAccessStream();
try
{
await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
var photoOrientation = ConvertOrientationToPhotoOrientation(GetCameraOrientation());
await ReencodeAndSavePhotoAsync(stream, photoOrientation);
}
catch (Exception ex)
{
Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString());
}
With:
private static async Task ReencodeAndSavePhotoAsync(IRandomAccessStream stream, PhotoOrientation photoOrientation)
{
using (var inputStream = stream)
{
var decoder = await BitmapDecoder.CreateAsync(inputStream);
var file = await KnownFolders.PicturesLibrary.CreateFileAsync("SimplePhoto.jpeg", CreationCollisionOption.GenerateUniqueName);
using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } };
await encoder.BitmapProperties.SetPropertiesAsync(properties);
await encoder.FlushAsync();
}
}
}
Have a closer look at the sample to see how to get the orientation of the camera in the first place (a call to it is being made in the first snippet I posted).
Or, if you prefer a video, you can watch the camera session from the recent //build/ conference, which includes a little bit of a walkthrough through some camera samples.
you can change the aspect ratio for your video preview & captured photo by setting in the MediaCapture.VideoDeviceController.
Also, you can set your video preview upright by using the following code.
MediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
I have answered a similar questions in the another post in the link below. Hope it helps.
https://stackoverflow.com/a/29875992/4672579

Windows Phone 8 Save Image from byte[]

I have created a basic steganography application for windows phone 8 that works fine on the emulator but I get an error when using a device. With the emulator I can save a byte[] as an image as shown below:
byteArrayTextandImage = EncodeText(byteArrayInputImageEncode,byteArrayInputText, 4096);
using (var s = new MemoryStream())
{
Picture pic = library.SavePicture(Guid.NewGuid().ToString(), byteArrayTextandImage);
}
I tried to get around it by using a WritableBitmap as shown below:
using (var s = new MemoryStream())
{
WriteableBitmap wb = new WriteableBitmap((BitmapSource)bmp);
wb.SaveJpeg(s, bmp.PixelWidth, bmp.PixelHeight, 0, 72);
s.Seek(0, SeekOrigin.Begin);
var pic = library.SavePicture(Guid.NewGuid().ToString() + ".jpg", s);
MessageBox.Show(pic.Name);
}
however this is modifying the image and removing the text I had encoded within it. Has anybody encountered a problem like this before? Why would the emulator work fine but not on a device? I have checked the manifest and I have the necessary capabilities checked.
Thanks for any help anybody can provide.

Categories

Resources