GithubHelp home page GithubHelp logo

Comments (15)

yangjieshao avatar yangjieshao commented on June 21, 2024 1

and how do i know the fps of the library that captures?

you can get VideoFormat by UsbCamera.GetVideoFormat(cameraIndex)
You can calculate it yourself based on VideoFormat.TimePerFrame
image

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

And maybe add like an event when a camera new frame is captured? and wait for the event to end before sending the next one

from usbcamera.

secile avatar secile commented on June 21, 2024

Thank you for your request.

I can't decide right now whether I will comply with your request.
For the time being, using GetPixel/SetPixel is very slow. So you had better use LockBits instead of that.
See example below. Is this can be solution of your problem?

private byte[] ExtractBitmapData(Bitmap bmp)
{
    var rect = new Rectangle(Point.Empty, bmp.Size);
    var data = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    var bytes = Math.Abs(data.Stride) * bmp.Height;
    var result = new byte[bytes];
    System.Runtime.InteropServices.Marshal.Copy(data.Scan0, result, 0, result.Length);
    return result;
}

Be careful, the order of color is BGR. (not RGB).

from usbcamera.

secile avatar secile commented on June 21, 2024

As to an event when a new frame is captured.

Are you using WinForms, or WPF?
If WinForms, You can subscribe PreviewCaptured callback.
(If WPF, PreviewCaptured can be used only for show preview with DataBinding.)
(The difference between WinForms and WPF is, WinForms pass new Bitmap every call, WPF pass single bitmap instance.)

Is this can be solution of your problem?

camera.PreviewCaptured += (bmp) =>
{
    // called here when every preview captured.
};

from usbcamera.

yangjieshao avatar yangjieshao commented on June 21, 2024

image
Perhaps you want to directly obtain this Buffer to reduce unnecessary bitmap conversions?

from usbcamera.

secile avatar secile commented on June 21, 2024

Hello, @yangjieshao.
Maybe, you are right.
But, I'm not sure if this feature is necessary for many other users.
And I think it's difficult to switch between Func GetBitmap and Func<byte[]> GetBitmap to implement this functionality.

@Mrgaton
For now, I make your custum version of UseCamera.cs. Could you try this?
GetBitmap() returns byte[] (not Bitmap.)
UsbCameraForIssue#29.zip

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

Or maybe add a function to get buffer instead of get bitmap

from usbcamera.

secile avatar secile commented on June 21, 2024

@Mrgaton

I recently sent your custom version of UsbCamera.cs last day.
Is it satisfied your requirement?

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

Can you add like a link like a credit on the start of the class with the link to you github repository

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

@Mrgaton

I recently sent your custom version of UsbCamera.cs last day. Is it satisfied your requirement?

yes but as i said i think it would be good to have two functions one to get bitmap and another to get it as byte array

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

like

Camera.GetBitmapRaw()

and

Camera.GetBitmap()

cause i need both

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

and how do i know the fps of the library that captures?

from usbcamera.

secile avatar secile commented on June 21, 2024

@Mrgaton

Can you add like a link like a credit on the start of the class with the link to you github repository

Thank you, I will do.

Camera.GetBitmapRaw()
Camera.GetBitmap()
cause i need both

This library has several ways to get image data.

  • GetBitmap()
  • PreviewCaptured
  • StillImageCaptured

I wouldn't add a function to get a buffer just to GetBitmap() only.
To do this, I will add new conditional compilation symbol to switch type of image data.
Converting byte array to Bitmap is easy.

private static Bitmap BufferToBitmap(byte[] buffer, int width, int height)
{
    var result = new Bitmap(width, height);
    var bmpData = result.LockBits(new Rectangle(Point.Empty, result.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpData.Scan0, buffer.Length);
    result.UnlockBits(bmpData);
    return result;
}

from usbcamera.

secile avatar secile commented on June 21, 2024

@Mrgaton

I modified UsbCameraForIssue#29. Could you test it?
UsbCameraForIssue#29_Rev2.zip

  • fix problem that byte array image data is upside down.
  • add new conditional compilation symbol 'USBCAMERA_BYTEARRAY' to change image type from Bitmap to byte array.

When you define conditional compilation symbol 'USBCAMERA_BYTEARRAY',
GetBitmap returns byte array instead of Bitmap. (and PreviewCaptured, StillImageCaptured passes data of byte array.)

To be precise, GetBitmap returns IEnumerable<byte>.
The data type is actually a byte array, so cast it to byte array before use it.

button1.Click += (s, ev) =>
{
    var buf = (byte[])camera.GetBitmap();
    var bmp = BufferToBitmap(buf, camera.Size.Width, camera.Size.Height);
    pictureBox2.Image = bmp;
};

private static Bitmap BufferToBitmap(byte[] buffer, int width, int height)
{
    var result = new Bitmap(width, height);
    var bmpData = result.LockBits(new Rectangle(Point.Empty, result.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpData.Scan0, buffer.Length);
    result.UnlockBits(bmpData);
    return result;
}

If you have any questions, please ask.

from usbcamera.

Mrgaton avatar Mrgaton commented on June 21, 2024

And why not the clas uses always the byte array but the función getbitmap gets the byte array and transforma It to a bitmap like

public Bitmap GetBitmap()
{
if (Buffer == null) return BitmapBuilder.EmptyBitmap;

            lock (BufferLock)
            {
                return BmpBuilder.BufferToBitmap(Buffer);
            }
        }

public byte[] GetBitmap()
{
return buffet;
}

And i think It would be perfect living both together

from usbcamera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.