device.Capture("filename.jpg") creates an invalid file!
See original GitHub issueDescribe the bug
According to the docs here - https://github.com/dotnet/iot/blob/main/src/devices/Media/README.md#videodevice - this should save a jpg to a file:
VideoConnectionSettings settings = new VideoConnectionSettings(busId: 0, captureSize: (2560, 1920), pixelFormat: PixelFormat.YUYV);
using VideoDevice device = VideoDevice.Create(settings);
// Capture static image
device.Capture("/home/pi/jpg_direct_output.jpg");
but the file is created corrupted and/or in wrong format.
Steps to reproduce
just run your code or provide a complete working example.
Versions used
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Platforms>AnyCPU;ARM64</Platforms>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
...
<ItemGroup>
...
<PackageReference Include="Iot.Device.Bindings" Version="2.0.0" />
<PackageReference Include="System.Device.Gpio" Version="2.0.0" />
</ItemGroup>
then I build and publish it to Raspberry Pi 3:
dotnet publish -r linux-arm --self-contained True -c Debug -p:PublishSingleFile=true -p:GenerateRuntimeConfigurationFiles=true
but THIS code works
(all dependencies are installed)
private void CaptureThePic(CancellationToken cancellationToken)
{
/*
*
* https://github.com/dotnet/iot/blob/main/src/devices/Media/README.md
*
*/
_logger.LogInformation("Capturing the pic...");
string picName = DateTimeOffset.UtcNow.ToString("s").Replace(":", "");
string picFileName = $"{PICS_FOLDER_S}/p{picName}.jpg";
/*
THIS DOES NOT WORK
VideoConnectionSettings settings = new(busId: 0, captureSize: (2560, 1920), pixelFormat: PixelFormat.YUYV);
using VideoDevice device = VideoDevice.Create(settings);
device.Capture(picFileName);
*/
VideoConnectionSettings settings = new(0, (1920, 1080), PixelFormat.YUV420);
using VideoDevice device = VideoDevice.Create(settings);
// Capture static image
_logger.LogInformation($"Saving pic as '{picFileName}'...");
// Get image stream, convert pixel format and save to file
byte[] imgBytes = device.Capture();
using MemoryStream ms = new(imgBytes);
Color[] colors = VideoDevice.Yv12ToRgb(ms, settings.CaptureSize);
using (Bitmap bitmap = VideoDevice.RgbToBitmap(settings.CaptureSize, colors))
{
if (cancellationToken.IsCancellationRequested) return;
bitmap.Save(picFileName, ImageFormat.Jpeg);
}
if (cancellationToken.IsCancellationRequested) return;
_logger.LogInformation("Saved.\n");
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:21 (20 by maintainers)
Top Results From Across the Web
How to Fix “An Unknown or Invalid JPEG Marker Type is ...
Method 2: Rename the image file · Right-click on the JPEG photo · Select Rename · Change name and enter extension to JPEG...
Read more >"Uncaught ImagickException: Invalid filename" on some ...
"Problems with higher resolution files and renaming the files makes no difference" indicates to me it is a problem with the file.
Read more >Invalid JPEG Marker error | Opening images
Solution 1: Make sure that the filename extension matches the actual file type. It's important that the extension you add to the filename...
Read more >What do I do if I get an "Invalid File Format" when trying to ...
If you receive an "Invalid File Format" when trying to upload your image on a ticket to Customer Support, this indicates that the...
Read more >Windows - How to remove 'invalid filename' error
I have a file on Windows that is causing an Out Of Sync on the folder. I'm not sure how the file got...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure @CodedBeard, that would be great. I did not have much time to make other tests today. As soon as we see this compatibility layer working decently, I will make a pull request with this and other changes I already made on my side.
These are the modified PInvokes that I currently declared. All these calls in the bindings should not point to
Libc
anymore. In the pull request I’ll make sure that this is transparent to the user of course.@raffaeler cool, as I couldn’t get any of those settings to work either 😃