This question already has answers here:
Including Images with an executable jar
(2 answers)
Closed 9 years ago.
I want to read a bunch of image files from my current package because I want to get access to the files when my package which includes my image files is exported to others' computer.
I just want to make sure my program can read the images no matter if the package is in my computer.
I tried
File file = new File("images.jpg"); // It is wrong because the path is wrong.
// I want to assign the image as BuffuredImage
BufferedImage dealerCardImage1 = ImageIO.read(file);
I was wondering how the path of the files should be. What should I do?
put your image file inside the package of current class file and try this:
BufferedImage dealerCardImage1 = ImageIO.read(getClass().getResourceAsStream("images.jpg"));
Note that, getClass().getResourceAsStream(path) returns an InputStream that points to a path that starts from current package.
For example if you have a class file named HelloWorld inside package com.example, then HelloWorld.class.getResourceAsStream("images.jpg") returns an InputStream to image with this path: com/example/images.jpg
if you read from file use
Image image = new Image();
image = ImageIO.read(file);
// Read from an input stream
InputStream is = getClass()
.getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");
image = ImageIO.read(is);
//if you read from url
// Read from a URL
URL url = new URL("http://hostname.com/image.gif");
image = ImageIO.read(url);
ImageIO.read(file); will return null if no registered ImageReader is not found.
Please check whether you have registered any ImageReader or not.
I think this code snippet could help you
File file = new File("images.jpg"); // Assuming images.jpg in my working directory
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file
You just need to wrap the file into an FileInputStream and then pass it to read()
Related
I am trying to read and write an image dataset in Hadoop using java. I am only convenient with the normal bufferImage.
folder = new File("");
img = ImageIO.read(f);
ImageIO.read accepts an InputStream
You can get an InputStream from an HDFS file using the FileSystem class's open function
This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
I have this line:
//Creating an image
Image image = new Image(new FileInputStream("C:\\images\\image.png"));
I would like to change to this line, I would like to get the image from resources in IntelliJ and not from my PC's C drive, for example, but it doesn't work:
Image image = new Image(new FileInputStream("file:src/main/resources/images/image.png"));
or
Image image = new Image(getClass().getResource("file:src/main/resources/images/image.png").toExternalForm());
I have an exception:
Caused by: java.lang.RuntimeException: Exception in Application start method
The path is determined from the classpath (= root) which is the case of src/main/resources.
Thus
Image image = new Image(getClass().getClassLoader().getResourceAsStream("/images/image.png"));
...should work.
You get get some more details here.
You simply need do like this.
In project put your images:
ProjectName/image.png
example of the project
and then
Image image = new Image(new FileInputStream("image.png"));
This question already has answers here:
How To Convert A Bitmap Image To Uri
(2 answers)
Closed 3 years ago.
I'm using a library that returns a bitmap image if I want to share this image to another application or send it via email? I don't have the Uri of the image then what should I do
It all depends on your environment:
- you can get hand over the complete image (e.g. an attachment in an email)
- if you have an solution which could be accessed from the target, then you can create your own uri. On a local system, you could store the image in a file. Or if you have a web application, you might want to make the image available so that you can provide an url to the image, too)
So it mainly depends on the whole Szenario: where does the image come from, how big is it? What is the target? ....
And sorry, that I do Not write this as a comment - need a reputation of 50 to write comments first ....
convert your bitmap into a file:
String path = Environment.getExternalStorageDirectory();
File file = new File(path + "/image_name.jpg");
OutputStream fOut = new FileOutputStream(file);
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
Get the URI from the file:
Uri yourUri = Uri.fromFile(file);
Addition: for android 7.0+ you need a different way to get URI from file.
Saving file to the default main directory is only an example but maybe not the best practice.
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 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.