How to take single snapshots from a webcam? - java

I want to take a snapshot with my webcam using java and save it to a jpg file. What are the steps needed to do so? A tutorial would be greatly appreciated.
Greetings,
Burkhard

the JMF (Java Media Framework) is a good starting point. However, I did not succeed with it.
I finally found the solution here.
The important part being:
Buffer buf = frameGrabber.grabFrame();
// Convert frame to an buffered image so it can be processed and saved
Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));
buffImg = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_RGB);
//TODO saving the buffImg

what you are looking for might be the Java Media Framework (JMF).
See the Sun Tutorial. I hope that helps.

I prefer using JMyron instead of JMF. JMyron is easy to use for accessing webcam. To save the captured image you just need to save the BufferedImage using ImageIO.write(); this blog post How To Use Webcam Using Javais usefull to start using JMyron.

Try webcam-capture project.
This code will take a snapshot from webcam (embedded, connected to USB or IP camera) and save it into JPG file:
Webcam webcam = Webcam.getDefault();
webcam.open()
BufferedImage image = webcam.getImage();
ImageIO.write(image, "JPG", new File("test.jpg"));

Related

The Thumbnailator library converts image to cmyk

I'm reading an image at full size with correct orientation with the Thumbnailator library which honors EXIF orientation flags.
InputStream is = new BufferedInputStream( new FileInputStream(filepath) );
BufferedImage image = Thumbnails.of(is).scale(1).asBufferedImage();
ImageIO.write(image, "jpg", new File(filepath));
But after converting the colorls are vrong so I checked the color model with XnView and it shows jpeg cmyk.
The original image was rgb. So why colors are wrong after using Thumbnailator library?
I was facing the same problem. I resolved the issue by saving it as PNG. Thumbnailator has got several issues and like the version (0.x.x) suggests there is no stable version out yet.
You can find the detailed solution at this link

Capture Image through webcam on JSP Page and store image in folder in java

How do you capture an image through the user's webcam and save it to file in the default image folder as well as into a database? I'm using JSP and Java in my web app.
It is very easy with OpenCV & JavaCV libraries , and here is the code snippet to capture image from webcam & save to disc.
IplImage img; // Image format provided with JavaCV APIs
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); // Camera Device ID (0 for built in , 1 for external etc)
grabber.start();
img = grabber.grab();
String imageName="images_name.jpg";
cvSaveImage(imageName, img);
Take a look at this website :
http://osama-oransa.blogspot.com.br/2012/06/accessing-webcam-within-jsp.html
Essentially:
Use a Jquery Webcam Plugin: http://www.xarg.org/project/jquery-webcam-plugin/%20
Use AJAX to send the data to Java.
Get the byte[] and save it.

How to read png images form inputstream in java

There are 2 applications. One application act as server and sends continuously screen shot of desktop by using the following code.
Robot robot=new Robot();
OutputStream os;
BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", os);
The second application is Android application acts a client application and has to read continuously the above image stream from inputstream.
Could please help me to read the png images from inputstream in the client application.
Thanks & Regards
Mini.
In client application, read the InputStream via Socket.getInputStream() method.
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
BufferedImage image = ImageIO.read(in);
Android SDK does not support the method ImageIO.read(). Even if you can compile your code, your android application will get crashed and have error about missing libraries like this:
could not find method javax.imageio.imageio.read
What I suggest is using bitmapping instead of this...

How do we convert WMF/EMF (MS metafiles) into standard images like JPG or PNG using any Java API?

I have been stuck in converting WMF/EMF images into standard image format such as JPG or PNG using Java.
What are the best options available?
The Batik library is a toolkit to handle SVG in Java. There are converters included like WMFTranscoder to convert from WMF to SVG and JPEGTranscoder and PNGTranscoder to convert SVG to JPEG/PNG. See Transcoder API Docs for more details.
Another alternative is ImageMagick. It's not Java but has Java bindings: im4java and JMagick.
wmf is a vector file format. For best results, convert them to .svg or .pdf format.
I did it in two stages
1) wmf2fig --auto XXXX.wmf
2) fig2pdf --nogv XXXX.fig
I created a python script for bulk conversion
import subprocess as sbp
a = sbp.Popen("ls *.wmf",shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
filelist = a.communicate()[0].splitlines()
for ele in filelist:
cmdarg = 'wmf2fig --auto '+ ele.rsplit('.',1)[0]+'.wmf'
a = sbp.Popen(cmdarg, shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
out = a.communicate()
for ele in filelist:
cmdarg = 'fig2pdf --nogv '+ ele.rsplit('.',1)[0]+'.fig'
a = sbp.Popen(cmdarg, shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
out = a.communicate()
cmdarg = 'rm *.fig'
a = sbp.Popen(cmdarg, shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
out = a.communicate()
If you are deploying your application in a Windows environment, then SWT can handle the conversion for you.
Image image = new Image(Display.getCurrent(), "test.wmf");
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
try(FileOutputStream stream = new FileOutputStream("test.png"))
{
loader.save(stream, SWT.IMAGE_PNG);
}
image.dispose();
The purpose of SWT is to provide a Java wrapper around native functionality, and in this case it is calling the windows GDI directly to get it to render the WMF.
I've created some wrappers around the Batik package (as mentioned by vanje's answer) some time ago, that provides ImageIO support for SVG and WMF/EMF.
With these plugins you should be able to write:
ImageIO.write(ImageIO.read(wmfFile), pngFile, "png");
Source code on GitHub.
While the ImageIO plugins are convenient, im4java and JMagick might still have better format support.
Here is one way.
Get (or make) a Java component that can render the files in question.
Create a BufferedImage the same size as the component needs to display the image.
Get the Graphics object from the BufferedImage.
Call renderComponent.paintComponent(Graphics)
Save the image using one of the ImageIO.write() variants.
See my answer to Swing: Obtain Image of JFrame for steps 2-5. Step 1. is something I'd ask Google about.

Display Image from Server in Java

I am new in Java programming. My query is that I am having an image which is present on a server and I want to display that image inside the JFrame. I tried using the Image class but that seems to be not working.
Please Note: I don't want to use applets for this, so is there some other method by which this can be done?
Thanks & Regards,
Assuming that it's a public accessible webserver, you can use URL#openStream() to get an InputStream out of an URL.
InputStream input = new URL("http://example.com/image.png").openStream();
Then you can just create the BufferedImage with help of ImageIO#read() the usual way.
BufferedImage image = ImageIO.read(input);
// ...

Categories