question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

device.Capture("filename.jpg") creates an invalid file!

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Comments:21 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
raffaelercommented, Jun 15, 2022

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.

    [DllImport("v4l2-compat.so", SetLastError = true)]
    internal static extern int open([MarshalAs(UnmanagedType.LPStr)] string pathname, Interop.FileOpenFlags flags);

    [DllImport("v4l2-compat.so")]
    public static extern int close(int fd);

    [DllImport("v4l2-compat.so", SetLastError = true)]
    public static extern int ioctl(int fd, int request, IntPtr argp);

    [DllImport("v4l2-compat.so", SetLastError = true)]
    public static extern IntPtr mmap(IntPtr addr, int length, MemoryMappedProtections prot, MemoryMappedFlags flags, int fd, int offset);

    [DllImport("v4l2-compat.so")]
    public static extern int munmap(IntPtr addr, int length);

    public enum MemoryMappedProtections
    {
        PROT_NONE = 0x0,
        PROT_READ = 0x1,
        PROT_WRITE = 0x2,
        PROT_EXEC = 0x4
    }

    [Flags]
    public enum MemoryMappedFlags
    {
        MAP_SHARED = 0x01,
        MAP_PRIVATE = 0x02,
        MAP_FIXED = 0x10
    }
1reaction
CodedBeardcommented, Jun 14, 2022

@raffaeler cool, as I couldn’t get any of those settings to work either 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found