Can I copy files into jar?
Im getting error on this one:
BufferedImage img;
img = ImageIO.read(MatrixMaker.class.getResourceAsStream("/resource/"+filemlg));`
I want to make arrays filled up with RGB form images . I need this images in rs folder because function need this getResourceAsStream.
You can get the input stream and convert to BufferedImage
InputStream is = this.getClass().getResourceAsStream("/resource/"+filemlg);
BufferedImage imBuff = ImageIO.read(is);
Related
I am reading a gif image from internet url.
// URL of a sample animated gif, needs to be wrapped in try-catch block
URL imageUrl = new Url("http://4.bp.blogspot.com/-CTUfMbxRZWg/URi_3Sp-vKI/AAAAAAAAAa4/a2n_9dUd2Hg/s1600/Kei_Run.gif");
// reads the image from url and stores in BufferedImage object.
BufferedImage bImage = ImageIO.read(imageUrl);
// creates a new `java.io.File` object with image name
File imageFile = new File("download.gif");
// ImageIO writes BufferedImage into File Object
ImageIO.write(bImage, "gif", imageFile);
The code executes successfully. But, the saved image is not animated as the source image is.
I have looked at many of the stack-overflow questions/answers, but i am not able to get through this. Most of them do it by BufferedImage frame by frame which alters frame-rate. I don't want changes to the source image. I want to download it as it is with same size, same resolution and same frame-rate.
Please keep in mind that i want to avoid using streams and unofficial-libraries as much as i can(if it can't be done without them, i will use them).
If there is an alternative to ImageIO or the way i read image from url and it gets the thing done, please point me in that direction.
There is no need to decode the image and then re-encode it.
Just read the bytes of the image, and write the bytes, as is, to the file:
try (InputStream in = imageUrl.openStream()) {
Files.copy(in, new File("download.gif").toPath());
}
I have an int array of color r,g and b values. And I would like to encode them in a image file. Is there an easy method in android to write this data to an image? Also which image format should I use for this, png?
Create a bitmap using your int array like this using Bitmap.createBitmap:
int[] array; // array of int RGB values e.g. 0x00ff0000 = red
Bitmap bitmap = Bitmap.createBitmap(array, width, height, Bitmap.Config.ARGB_8888);
Then write it out using Bitmap.compress:
outStream = new FileOutputStream(filepath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
You can call Environment.getExternalStorageDirectory() to get a folder on external storage where you can save the file, if that's where you want to save it. You can get the path with get File.getAbsolutePath(), e.g:
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/image.png";
You need the WRITE_EXTERNAL_STORAGE permission defined in your AndroidManifest.xml to be able to write to files on external storage.
There are pure java implementations for reading and writting images:
Image processing library for Android and Java
Probably some will work in android out of the box
I believe ImageIO is available in Android. The ImageIO API provides methods to read the source image and to write the image in the new file format.
To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.
//Create file for the source
File input = new File("c:/temp/image.bmp");
//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);
Once you have the BufferedImage, you can write the image as a PNG. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "png".
//Create a file for the output
File output = new File("c:/temp/image.png");
//Write the image to the destination as a PNG
ImageIO.write(image, "png", output);
I have an image, I read the image, add a few things to image (like some text etc).
All this I do inside a JPanel.
Now, I want to save the resulting image to a .png file.
I think, there is a way to do this for a buffered image using ImageIO.write()
But I cannot convert the dynamically created image to a BufferedImage.
Is there a way I can go about this ?
You can use the Screen Image class.
It will create a BufferedImage of your JPanel. The class also has code to write the image to a file.
All this I do inside a JPanel.
Do it instead in another BufferedImage displayed in a JLabel. The code can get a Graphics2D object using BufferedImage.createGraphics() method. Paint the image and text to the new Graphics2D instance and you then the new image can be saved directly, along with changes.
Use Following method it worked for me...
void TakeSnapShot(JPanel panel,String Locatefile){
BufferedImage bi = new BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);
panel.paint(bi.createGraphics());
File image = new File(Locatefile);
try{
image.createNewFile();
ImageIO.write(bi, "png", image);
}catch(Exception ex){
}
}
How to convert Buffered image into a file object.
My function actually needs to return a file object . The imgscalr resizing function returns a BufferedImage after resizing.so How to convert it into a file object.
Here is an example of writing to a PNG file:
ImageIO.write(yourImage, "PNG", "yourfile.png");
You must import ImageIO (javax.imageio) first, however.
Then you can get the File object for the image with new File("yourfile.png");.
You could put this in a function for ease of use; here is an example:
public File imageToFile(BufferedImage img, String fileName) {
if (!(fileName.endsWith(".png"))) fileName += ".png";
ImageIO.write(img, "PNG", filename);
return new File(fileName);
}
Here is a link to the docs.
You cannot make a file object without saving... well, a file. You could put it in a temporary directory and delete it when you are done using it, though.
I am reading a 100 MB picture into my app. It works fine inside Eclipse, but not when I export project to a JAR. Then, I get "Can't read input file!"
Since I need to edit it, I used BufferedImage.
private String str = "images/1.png";
BufferedImage imageMap;
//in constructor
imageMap = ImageIO.read(new File(str));
I have tried this, but the project image does not load inside Eclipse:
imageMap = ImageIO.read(this.getClass().getClassLoader().getResource(str));
Check you working directory if the image is loaded from the file system. Then you see if your relative path "images/1.png" is valid. Or you directly check the path of your png
System.out.println(new File("."));
File f = new File("images/1.png");
System.out.println(f.getAbsolutePath());