Encoding image file - java

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);

Related

How can I add a profile picture to a user in liferay using java?

I wish to create a new user programmatically and giving this user a portrait. I'm using a csv file, to loop through my code, which has a number of entries. Their fullname, their email adres and a url which links to an image is all in this csv file.
The users are made but they don't get a profile picture, however if i use a path to my local machine it works just fine. Is there a way i can use a url and save the image as an object in java?
My code so far using a local file:
// Getting image from local path
File sourceImage = new File("C:\\Users\\path_to_image");
BufferedImage img = ImageIO.read(sourceImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
byte[] bytes = baos.toByteArray();
// Adding profile picture to newly made user
UserLocalServiceUtil.updatePortrait(user.getUserId(), bytes);
I do not want to have to download the pictures because there are quite a lot of them, but if that is the only way I'll have to of course.
Any help would be much appreciated!

Write animated-gif stored in BufferedImage to java.io.File Object

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());
}

Can i copy file into jar

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);

Read region from very large image file in Java

Is there a Java library that can read regions of very large image (e.g. JPEG) files (> 10,000 x 10,000 pixels) without keeping the whole image in memory.
Or alternatively, which Java library is capable of handling very large image files with a minimum of overhead.
Standard ImageIO allows you to read regions of (large) images without reading the entire image into memory first.
Rectangle sourceRegion = new Rectangle(x, y, w, h); // The region you want to extract
ImageInputStream stream = ImageIO.createImageInputStream(input); // File or input stream
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(stream);
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(sourceRegion); // Set region
BufferedImage image = reader.read(0, param); // Will read only the region specified
}
You can use, for example, RandomAccessFile to read from the middle of the file:
but the issue is that whole jpeg image is compressed after DCT quantization (http://www.fileformat.info/mirror/egff/ch09_06.htm), so I don't think that it is possible to read a fragment without reading whole file to memory.
You can use a BufferedImage to do what you need.
// Set these variables according to your requirements
int regionX, regionY, regionWidth, regionHeight;
BufferedImage image = ImageIO.read(new File("/path/to/image.jpg"));
BufferedImage region = image.getSubimage(regionX, regionY, regionWidth, regionHeight);
And then you can process the region subimage however you want.

Image resizing using imgscalr API in java

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.

Categories

Resources