Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide the more available size and format for the getImage() #15

Closed
He-Pin opened this issue Jan 9, 2013 · 7 comments
Closed

provide the more available size and format for the getImage() #15

He-Pin opened this issue Jan 9, 2013 · 7 comments
Assignees
Milestone

Comments

@He-Pin
Copy link

He-Pin commented Jan 9, 2013

thanks first

when i use your api ,i always grab the original bufferedImage from the webcam.then i use the imageIO to wirte it to an BytearrayOutputStream,so i think how about.you provide some othere method such as

webcam.getImage(ImageType.JPG)
which can return an Jpeg image,and how about return bytebuffer or byte[] too.

i still implement an grabber which use an schedule service to grab the image with an listener .
i think that you can provide it too.

i think if you can provide the google's webp image type ?

i just wanna your great API to be more easy to use,so ,sorry for so many request.
https://gist.github.com/4489904

an i have found this:
http://code.google.com/p/thumbnailator/

which can scale up and scale down the image,it's fast and high quality

@sarxos
Copy link
Owner

sarxos commented Jan 9, 2013

Hi,

I think this is very good idea, even if it requires not a small development effort.

In case of formats such as JPG, PNG, BMP and other basic ones supported by ImageIO API, I think it will not be a problem, however in case of e.g. WebP, I would rather like to create separate sub project, to avoid adding more dependencies to Webcam Capture API itself.

I will think about how this could be implemented in the best way.

Thanks again!

@He-Pin
Copy link
Author

He-Pin commented Jan 9, 2013

@sarxos ,i write this to an byteArrayOutPutStream,with the ImageIO,and then get the bytes from the byteArrayOutputStream.
here is the gist

    public class GrabTask implements Runnable{
        protected final Webcam webcam;
        protected final ByteArrayOutputStream byteArrayOutputStream;
        protected final GrabberListener graberListener;


        public GrabTask(Webcam webcam,
                ByteArrayOutputStream byteArrayOutputStream,
                GrabberListener graberListener) {
            super();
            this.webcam = webcam;
            this.byteArrayOutputStream = byteArrayOutputStream;
            this.graberListener = graberListener;
        }


        @Override
        public void run() {
            byteArrayOutputStream.reset();
            BufferedImage bufferedImage = webcam.getImage();

            BufferedImage compressedImage = ImageUtils.convertToType(
                    bufferedImage, BufferedImage.TYPE_3BYTE_BGR);
        //  System.out.println("image type :"+bufferedImage.getType());

            if (orginData) {
                graberListener.onGrabed(compressedImage);
                return;
            } 

            try {
                ImageIO.write(compressedImage, "JPG", byteArrayOutputStream);
                //
                graberListener.onGrabed(byteArrayOutputStream.toByteArray());
                //graberListener.onGrabed(byteArrayOutputStream.toByteArray());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

@sarxos
Copy link
Owner

sarxos commented Jan 9, 2013

Just a note to your gist - I think you don't have to convert received image to TYPE_3BYTE_BRG. Resultant image is TYPE_CUSTOM due to predefined ColorModel and SampleModel used, but in fact it's classic 3-byte RGB type, so you can save with ImageIO directly, without conversion.

@He-Pin
Copy link
Author

He-Pin commented Jan 9, 2013

really ?! i will have an test !
thantks @sarxos ,i just use do this follow the code of the xuggler,its example said that should use the 3 byte RGB,so i will have an test now.

update:
wow!!!

that's great i just remote the code ,and it still works fine.

cause i need to encode the buffered image to h264 packet,so i just follow the example, and decode the packet,
thanks again.

i am newbie for video~~
thanks 💯

@ghost ghost assigned sarxos Jan 10, 2013
@sarxos sarxos closed this as completed in 6849d97 Jan 14, 2013
@sarxos
Copy link
Owner

sarxos commented Jan 14, 2013

Hi @hepin1989,

I didn't want to mess with Webcam class API to keep it clean so I've created several utility methods you can use to capture image bytes in various formats, e.g.:

byte[] bytes = WebcamUtils.getImageBytes(webcam, "jpg");
ByteBuffer buffer = WebcamUtils.getImageByteBuffer(webcam, "jpg");

And:

byte[] bytes1 = ImageUtils.toByteArray(webcam.getImage(), "jpg");
byte[] bytes2 = ImageUtils.toByteArray(webcam.getImage(), "gif");
byte[] bytes3 = ImageUtils.toByteArray(webcam.getImage(), "webp");

The last example where WebP form,at is used can be done only when you have register ImageIO plugin supporting WebP encoding. It is available in SF:

EDIT: (forgot to add link)

http://sourceforge.net/projects/javavp8decoder/files/imageIO%20Plugin/

@sarxos
Copy link
Owner

sarxos commented Jan 14, 2013

I also had to remove resolutions from DefaultWebcamDevice because this class is hidden in OSGi runtime and therefore users will not be able to use it (will receive ClassNotFound error).

Instead I've created new enum called WebcamResolution. Please use it instead. Example:

Dimension size = WebcamResolution.VGA.getSize();

Sorry for all inconveniences, since, I guess you have already use it in your code.

@He-Pin
Copy link
Author

He-Pin commented Jan 15, 2013

@sarxos ,noop,do you know that,your code and design is nice and clean ,i love it.to create several utility methods is more good.

and the resolution is nice and clean too,i just wannt to metion you this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants