To capture screen shot in my java application i have write following code
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File("resources/img/screenshot.png"));
This is working successfully and capture screen shot but this is not working in windows 8 operating system. any one else who have face this type of problem and get soluction?
my application is install into the program file folder and the windows 8 not give permission to write there how i can write there now?
Do not write it there! OS manufacturers as well as Sun/Oracle have been saying for years not to write files to the application's installation directory. It is not only the wrong place to write them, but as you have discovered, does not provide write permissions for a typical Java app.
Instead put the screen-shot in user.home e.g. as seen in this answer.
you can do that without writing file in your local machine by using the following code
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] imageByte = null;
try
{
//To Get the the size of the screen.
Rectangle screenRect = new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize());
//To Store the scrreen shot in buffer.
BufferedImage capture = new Robot()
.createScreenCapture(screenRect);
//To Save the image on specific location.
ImageIO.write(capture, "png",bos);
bos.flush();
imageByte=bos.toByteArray();
bos.close();
// File file = new File("resources/img/screenshot.png");
MultipartEntity mpEntity = new MultipartEntity();
// ContentBody cfBody = new FileBody(file);
ContentBody cfBody = new ByteArrayBody(imageByte,"screenshot.png");
mpEntity.addPart("screenshot", cfBody);
}
send direct the byte array in plase of the image
Related
We are trying to download an image file from url https://test.com/images/123.jpg
URL url = new URL("https://test.com/images/123.jpg");
InputStream inputStream = url.openStream();
byte[] buffer = new byte[2048];
pResponse.setContentType("application/octet-stream");
pResponse.setHeader("Content-Disposition","attachment;filename=\""123.jpg\"");
while ((inputStream.read(buffer)) != -1) {
pResponse.getOutputStream().write(buffer);
}
pResponse.getOutputStream().flush();
pResponse.getOutputStream().close();
inputStream.close();
The downloaded file is corrupted. Click here for screenshot. When I tried to open the file with Notepad++, a empty line is appended at the beginning of the file.
On saving the file by removing the empty line at the beginning of the file, We are able to open the image successfully.
When I changed the code and not writing the bytes to pResponse.getOutputStream() then the image downloaded has an empty line.
So, How can we remove that empty line or reset the output stream to empty
Please correct me if I am wrong
Regards,
John
I do not know why empty line is getting appended, but when tries to create the same scenario of downloading a file i used this code and it is working fine:
URL url = new URL("http://localhost:8080/HTMLCS/Images/OCR.PNG");
BufferedImage img = ImageIO.read(url);
File file = new File("C:\\Users\\username\\Desktop\\RegressionTests\\OCR.png");
ImageIO.write(img, "png", file);
It may or may not be helpful to you but you should give it a try.
Please do let me know if i was not able to understand your question correctly.
I'm a beginner in Java and a geomatics student.
I'am using IntelliJ.
I would like to create a TIFF from a BufferedImage.
This is my code :
byte[] buffer = new byte[width * height];
ColorSpace cs = ColorSpace.getInstance( ColorSpace.CS_GRAY );
int[] nBits = { 8 };
ColorModel cm = new ComponentColorModel( cs, nBits, false, true,Transparency.OPAQUE, DataBuffer.TYPE_BYTE );
SampleModel sm = cm.createCompatibleSampleModel( width, height );
DataBufferByte db = new DataBufferByte( buffer, width * height );
WritableRaster raster = Raster.createWritableRaster( sm, db, null);
BufferedImage result = new BufferedImage( cm, raster, false , null );
File outputfile = new File( "saved.png" );
ImageIO.write( result, "png", outputfile );
A raster .png is create and it works well. But I want to create a .TIFF and ImageIO.write don't create TIFF (only png,bmp and jpeg). So I download the JAI (Java Advanced Imaging) here : http://download.java.net/media/jai/builds/release/1_1_3/
I upload it on my project and on Maven, but I don't know how to make a tiff simply... I try some snippets that I found on the internet but it don't work..
TIFFEncodeParam params = new TIFFEncodeParam();
FileOutputStream os = new FileOutputStream("PingsTiff.tiff");
javax.media.jai.JAI.create("encode", result, os, "TIFF", params);
The "TIFFEncodeParam" and "media" is not recognized...and I'm a real noob at programming..
Thanks
First of all, JAI comes with a set of ImageIO plugins, that will allow you to use ImageIO.write to write in TIFF format. But it requires the jai_imageio.jar to be on class path. I guess this is the JAR you are missing.
Also, the code you posted should work, if you have the imports and dependencies set up correctly. It's a little tricky because some parts of JAI requires native libraries that needs to be installed using the installer, and in the correct JRE, etc. Because of this, it's not a perfect fit with Maven (although certainly doable).
However, as you see from the download link in your question, JAI is a pretty much dead project (the latest update is from 2006).
Because of this lack of updates, bug fixes and support, along with the native parts and the license issues, I set up an open source project, aimed to provide at least as good file format support as JAI, with no native requirements and released under BSD license.
You can read about it, and the TIFF plugin in particular at the project home page. A little further down the page is down is download links, Maven dependency information etc.
When you have declared dependency on the TIFF plugin, you should be able to write your TIFF using plain ImageIO like this:
File outputfile = new File("saved.tif");
if (!ImageIO.write(result, "TIFF", outputfile)) {
// Beware, write is a boolean method, that returns success!
System.err.println("Could not write " + outputfile.getAbsolutePath() + " in TIFF format.");
}
I am creating .PNG file using BufferedImage with some test. Now after creating image I am trying to convert .PNG image to .TIF, which is working fine. Now once I create TIF image, I want to delete PNG image. But because of some reason, I am not able to do this. There is no any exception for this.
Here is my code
File pngFile = null;
FileOutputStream fOut = null;
try {
pngFile = new File("C:\\Test.PNG");
fOut = new FileOutputStream ("C:\\Test.TIF");
RenderedOp src = JAI.create("fileload", "C:\\Test.PNG");
TIFFImageEncoder encoder = new TIFFImageEncoder (fOut, null);
encoder.encode (src);
}catch(Exception e) {
}finally {
fOut.close();
System.out.println(pngFile.delete());
}
Well there's definitely no exception since your catch block is empty.
Something may be still holding a handle to the file, not allowing it to be deleted.
I would examine JAI.create, RenderedOp and the TiffEncoder.
Instead of providing the file path as string you can provide input stream and in finally first close the input stream and then delete the file. This may work.
I was facing same problem sometime before. The best way to do it in this is to first dispose the resources using the image object you have create, like below-
var image = Image.FromFile(pngTarget); // here pngTarget is my PNG file's name along with complete path.
// your code to convert png to tiff
.
.
.
at the end of the method you can write below -
image.Dispose(); // the image object I have created above
File.Delete(pngTarget); // delete the file
Also, don't forget to flush/close the memory stream, if using any.
Thanks.
I am trying to use the ImageIO class to save an image and then get the resource using an input stream. My problem is that I keep getting a NullPointerException whenever I try to create the input stream. If I simply go and put an image file in the class path, it works. Here is my code:
ImageIO.write(image, "png", new File("temp.png"));
InputStream imgIs = AptCap.class.getResourceAsStream("temp.png");
byte[] imgData = new byte[imgIs.available()]; // I get null here.
I have also tried specifying direct locations to files on the C drive for both of them, but I still get a null pointer exception. I would rather not do that anyway, but just keep it in the classpath (for purposes of multi OS support).
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // create OutputStream
ImageIO.write(image, "png", baos); // write to OS
InputStream imgIs = new ByteArrayInputStream(baos.toByteArray()); // grab bytes from OS
//..
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...