Captured photo with stripes - c#

I'm using MediaCapture class to take a photo with Windows Phone 8.1 Runtime. The code, where I take a picture looks like this:
// create a file
StorageFile photoFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFirstPhoto.jpg", CreationCollisionOption.ReplaceExisting);
// take a photo with choosen Encoding
await captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoFile);
The code is working quite fine, as I get a picture, but with strange stripes on left and right side:
I'm trying to find a solution for this problem, but without success. Am I missing something?
EDIT - Photos takes from build-in app are without stripes, so this seems not to be a problem with hardware.

Ok I've figured it out myself - it's a problem with resolution which is set as default when using MediaCapture. If you set maximum resolution just after Initializing MediaCapture then there will be no stripes:
// just after initialization
var maxResolution = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate(
(i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution);

I tried with the lowest resolution it gets the same stripes.
From MS Advance Camera sample, they use MediaCapture for preview but SilverLight API for capturing!
The MediaCapture has also memory leak issue each time when you call CapturePhotoToXXX API.
I wonder if they already know MediaCapture is badly managed on WindowsPhone 8.1 and this is the reason why they don't provide a Camera MediaCapture sample with RT APIs.

Related

Windows Phone 8.1 MediaComposition - Audio Too Fast When Stitching Videos

I am having a problem when trying to concatenate multiple videos together. Whenever I combine 2 or more videos, the audio is played at double speed, while the video plays out normally.
Below is the code. Am I missing something?
I get the same results when testing but cloning a single video or selecting multiple videos.
I have compared to the code example here (I am not trimming).
public static IAsyncOperation<IStorageFile> ConcatenateVideoRT([ReadOnlyArray]IStorageFile[] videoFiles, IStorageFolder outputFolder, string outputfileName)
{
return Task.Run<IStorageFile>(async () =>
{
IStorageFile _OutputFile = await outputFolder.CreateFileAsync(outputfileName, CreationCollisionOption.GenerateUniqueName);
MediaComposition _MediaComposition = new MediaComposition();
foreach (IStorageFile _VideoFile in videoFiles)
{
MediaClip _MediaClip = await MediaClip.CreateFromFileAsync(_VideoFile);
_MediaComposition.Clips.Add(_MediaClip);
_MediaComposition.Clips.Add(_MediaClip.Clone());
}
TranscodeFailureReason _TranscodeFailureReason = await _MediaComposition.RenderToFileAsync(_OutputFile);
if (_TranscodeFailureReason != TranscodeFailureReason.None)
{
throw new Exception("Video Concatenation Failed: " + _TranscodeFailureReason.ToString());
}
return _OutputFile;
}).AsAsyncOperation();
}
It looks like there are two issues. I have got this working by adding the following line:
MediaEncodingProfile _MediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
And changing the following line:
TranscodeFailureReason _TranscodeFailureReason = await _MediaComposition.RenderToFileAsync(_OutputFile);
To:
TranscodeFailureReason _TranscodeFailureReason = await _MediaComposition.RenderToFileAsync(_OutputFile, MediaTrimmingPreference.Fast, _MediaEncodingProfile);
The problem seems to be that RenderToFileAsync doesn't appear to work properly when using VideoEncodingQuality.HD720p or VideoEncodingQuality.HD1080p, both of these settings recreate the fast audio issue. Also, using VideoEncodingQuality.Auto seems to cause the encode to fail (although I think it is meant to use the default settings from the camera).
Additionally, I posted the problem on the Microsoft Partner Community Forums and their response was that encoding can fail on specific devices, e.g. in their tests video recorded on a Lumia 638 could not be encoded/concatenated even on other devices, but video from a HTC 8x, Lumia 920 and Lumia 930 could be encoded on all devices even the 638.
They suggested this was a device problem (firmware) not a Windows.Media.Editing API problem.

Decoding Barcode using Zxing library works on 1 tablet but does not work on another tablet

I have written a windows store app in XAML & C# to read image from tablet's webcam and decode the barcode using Zxing's lbrary. The code is working fine on a given tablet having an i5 processor while it fails to run on an actual tablet with 2MP camera and "Intel Baytrail Quad-Core" processor.
Any ideas on why this could happen?
Please let me know if you need to see my code for this issue ad I will share.
I am wondering how can the same code work on 1 tablet while fail on another tablet.
Thanks in advance for any help provided.
EDIT
Code used to scan the barcode and read as below - The last if/else block is what I get to. No exception raised :(
string barcodeData = string.Empty;
using (var imageStream = new InMemoryRandomAccessStream())
{
processingImage = true;
var encodingProperties = new ImageEncodingProperties();
encodingProperties.Subtype = "Jpeg";
encodingProperties.Width = 400;
encodingProperties.Height = 400;
await captureMgr.CapturePhotoToStreamAsync(encodingProperties, imageStream);
await imageStream.FlushAsync();
imageStream.Seek(0);
var bitmap = new WriteableBitmap(400, 400);
bitmap.SetSource(imageStream);
preview1.Source = bitmap; //preview1 is an Image control to display the captured image
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imageStream.Seek(0);
var bitmapDecoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, imageStream);
var data = await bitmapDecoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage
);
if (data != null)
{
BarcodeReader barcodeReader = new BarcodeReader();
var result = barcodeReader.Decode(
data.DetachPixelData(),
(int)bitmapDecoder.PixelWidth,
(int)bitmapDecoder.PixelHeight,
ZXing.RGBLuminanceSource.BitmapFormat.BGR32
);
if (result != null)
{
//Barcode found
}
else
//No data found.
}
}
I guess you are using ZXing.NET library.
Have you ever considered moving to another barcode scanner library?
Accessing the "ISSUES" section in ZXing.NET Library, you can see that there's a lot of bugs still opened for Windows Phone (and should be Window Store also).
http://zxingnet.codeplex.com/workitem/list/basic
One of it called my attention. Check out this comment:
While the WP samples all target Silverlight, you must not forget that the new WP8.1 base is WinRT - so I suggest you use the WinRT sample as a base.
I tried to do the same, but truth to be told, ZXing lacks a lot ATM for WinRT Universal Apps - it's slow, unreliable, and barely ever recognizes a thing.
http://zxingnet.codeplex.com/workitem/13311
I don't know how reliable this is, but the last time the project was updated was on April 7th!!!!
You should really consider changing you library!
Hi,
I made a lib for WinRT using ZXing & Imaging SDK.
It works well (but does not include any additional focus feature).
https://github.com/stepheUp/VideoScanZXing4WP81
There is a lib and a sample app that you can try.
It works for barcodes and QRCode (barcode by default but just change the optional parameter in the scan function code to use QRCode)
Hope it helps,
Stéphanie

Unity3d - 2 issues with Facebook Sharing in Window Phone

Everybody,I must say that I have post this question 3 times in Unity Community (include answer hub and its forum) but no one can solve it.
I use TakeScreenShot() function (it's in the InteractiveConsole example in FB SDK for Unity) to take a screenshot and post it to FB. But there are 2 problems appeared:
First: the screenshot that's captured is a gray blank like this: http://i7.minus.com/iXiHlCcSWaVfC.jpg
Second: No one can see my post except me although I set the photo to public.
How can I fix these problems?
Here is the code of TakeScreenShot() function:
private IEnumerator TakeScreenshot() {
yield return new WaitForEndOfFrame();
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] screenshot = tex.EncodeToPNG();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "InteractiveConsole.png");
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
FB.API("me/photos", Facebook.HttpMethod.POST, Callback, wwwForm);
Debug.Log("done");
}
I hope you solved your issues but I will answer anyways to your questions:
•First: the screenshot that's captured is a gray blank.
I think this works for Windows Phone only since very recently on Unity 4.5, update and try again.
Unity 4.5 Windows Phone Fixes
Grey Textures happen if you forget to call the Apply method. You will end up with a grey texture if forget to call. I see you are calling tex.Apply() but my guess is that you added it later and you may be seeing an old grey texture. Your code should work as it is. If you still get the grey Texture after updating to version 4.5 or later, try to wait a little time between the Apply and calling the EncodeToPNG(), maybe yield between those calls. Bear in mind that both are expensive operations.
•Second: No one can see my post except me although I set the photo to public.
That is because you have not go live (public) with you facebook app, during development only you will see you and your development team will see the messages. By the way fecebook will not approve hardcoded messages so even though this may work, and I know you based your code on facebook own smaples, you will need to allow the user to edit the message before posting.

Recording (or transforming after recording) video at non-standard size in Windows Phone 8

I am trying to capture MP4 video at a specific resolution in Windows Phone 8 (to be specific, 480x480). I know that I can't use sizes other than the presets, and 480x480 is not a preset. How do I transform a captured video (such as 640x480) and crop the top and bottom to make it 480x480? Any free or open source libraries (that run on Windows Phone) are welcome. Please don't answer with answers such as 'use an external server', I need an on-device solution.
Use the Windows.Phone.Media.Capture APIs and the AudioVideoCaptureDevice class
Second parameter for AudioVideoCaptureDevice.OpenAsync - see this link - is the resolution. And you can get the resolutions using AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensor)
EDIT: To set custom resolutions try AudioVideoCaptureDevice.SetCaptureResolutionAsync
EDIT 2: You could try something like the following to transform recorded video: (can't find where i got the code from soz to author!)
StorageFolder isoStore = await ApplicationData.Current.LocalFolder.GetFolderAsync("Shared");
var file = await isoStore.CreateFileAsync("foos1.wmv", CreationCollisionOption.ReplaceExisting);
using (var s = await file.OpenAsync(FileAccessMode.ReadWrite))
{
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
avDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back).Last());
VideoBrush videoRecorderBrush = new VideoBrush();
videoRecorderBrush.SetSource(avDevice);
viewfinderRectangle.Fill = videoRecorderBrush;
await avDevice.StartRecordingToStreamAsync(s);
Thread.Sleep(30000);
await avDevice.StopRecordingAsync();
}
new MediaPlayerLauncher()
{
Media = new Uri(file.Path, UriKind.Relative),
}.Show();

Instagram Photo Effects in Windows 8 metro apps using c#

I need to implement Instagram photo effects like amaro, hudson, sepia, rise, and so on. I know this article only use basic effects: http://code.msdn.microsoft.com/windowsdesktop/Metro-Style-lightweight-24589f50
Another way suggested by people are to implement Direct2d and then apply using that. But for that I would need to write C++ code, where I have zero experience.
Can anyone suggest some other way to implement Instagram effects in c#?
Is there any built in c++ file for these effects?
Please see this example from CodeProject : Metro Style Lightweight Image Processing
The above example contains these image effects.
Negative
Color filter
Emboss
SunLight
Black & White
Brightness
Oilpaint
Tint
Please note above example seems to be implemented on either developer preview or release preview of Windows 8. So you will get error like this
'Windows.UI.Xaml.Media.Imaging.WriteableBitmap' does not contain a
constructor that takes 1 arguments
So you have to create instance of WriteableBitmap by passing pixel height and pixel width of image. I have edited the sample and it is working for me. You have to change wb = new WriteableBitmap(bs); to wb = await GetWB();
StorageFile originalImageFile;
WriteableBitmap cropBmp;
public async Task<WriteableBitmap> GetWB()
{
if (originalImageFile != null)
{
//originalImageFile is the image either loaded from file or captured image.
using (IRandomAccessStream stream = await originalImageFile.OpenReadAsync())
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(stream);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
byte[] pixels = await GetPixelData(decoder, Convert.ToUInt32(bmp.PixelWidth), Convert.ToUInt32(bmp.PixelHeight));
cropBmp = new WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight);
Stream pixStream = cropBmp.PixelBuffer.AsStream();
pixStream.Write(pixels, 0, (int)(bmp.PixelWidth * bmp.PixelHeight * 4));
}
}
return cropBmp;
}
Let me know if you are facing any problem.

Categories

Resources