I am attempting to increase the quality of an image produced from a BufferedImage. The final aim is for a JPEG to be input (here it is retrieved from a file on the computer), be converted to a grayscale TIFF and then output as a byte array. I have included code to save the final image to the PC so it is easier to discern the problem.
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import java.awt.image.MultiPixelPackedSampleModel;
import java.awt.image.SampleModel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.media.jai.ImageLayout;
import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.LookupTableJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.operator.ErrorDiffusionDescriptor;
public class ByteConversionService {
private static ByteArrayOutputStream baos;
private static ImageWriter writer;
private static ImageOutputStream ios;
private static ImageWriteParam writeParam;
public static void main(String args[]) {
try {
convertBufferedImageToByteArray();
} catch (Exception e) {
}
}
private static byte[] convertBufferedImageToByteArray()
throws Exception {
byte[] convertedByteArray = null;
resourceSetup();
try {
File file = new File("../proj/src/image.jpg");
BufferedImage image = ImageIo.read(file);
convertImageToTif(image);
createImage(baos);
convertedByteArray = baos.toByteArray();
} finally {
resourceCleanup();
}
return convertedByteArray;
}
private static void resourceSetup() throws Exception {
baos = new ByteArrayOutputStream();
writer = ImageIO.getImageWritersByFormatName(
"tif").next();
ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writeParam = writer.getDefaultWriteParam();
writeParamSetUp(writeParam);
}
private static void writeParamSetUp(ImageWriteParam writeParam) {
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("CCITT T.4");
}
private static void convertImageToTif(BufferedImage image) throws Exception {
try {
BufferedImage blackAndWhiteImage = imageToBlackAndWhite(image);
writeToByteArrayStream(blackAndWhiteImage);
IIOImage iioImage = new IIOImage(blackAndWhiteImage, null, null);
writer.write(null, iioImage, writeParam);
} finally {
image.flush();
}
}
private static BufferedImage imageToBlackAndWhite(BufferedImage image) {
PlanarImage surrogateImage = PlanarImage.wrapRenderedImage(image);
LookupTableJAI lut = new LookupTableJAI(new byte[][] {
{ (byte) 0x00, (byte) 0xff }, { (byte) 0x00, (byte) 0xff },
{ (byte) 0x00, (byte) 0xff } });
ImageLayout layout = new ImageLayout();
byte[] map = new byte[] { (byte) 0x00, (byte) 0xff };
ColorModel cm = new IndexColorModel(1, 2, map, map, map);
layout.setColorModel(cm);
SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
surrogateImage.getWidth(), surrogateImage.getHeight(), 1);
layout.setSampleModel(sm);
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
PlanarImage op = ErrorDiffusionDescriptor.create(surrogateImage, lut,
KernelJAI.ERROR_FILTER_FLOYD_STEINBERG, hints);
BufferedImage blackAndWhiteImage = op.getAsBufferedImage();
return blackAndWhiteImage;
}
private static void writeToByteArrayStream(BufferedImage image) throws Exception {
ImageIO.write(image, "tif", baos);
}
private static void createImage(ByteArrayOutputStream baos) throws Exception {
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
ImageReader reader = (ImageReader) ImageIO.getImageReadersByFormatName(
"tif").next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage buffered = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffered.createGraphics();
g2.drawImage(image, null, null);
File file = new File("../proj/src/image2.tif");
ImageIO.write(buffered, "tif", file);
}
private static void resourceCleanup() throws Exception {
ios.flush();
ios.close();
baos.flush();
baos.close();
writer.dispose();
}
}
The current issue is that the final image is of low quality - zooming in shows a lot of white space between the pixels composing the picture. My understanding is this is possibly due to the dithering algorithm used (Floyd-Steinberg) so the image is not technically reproduced in grayscale.
I have attempted multiple solutions which I will post in the comments, but with no success. My question is if the final quality can be increased with my current solution or if my conversion to grayscale is flawed and the imageToBlackAndWhite method is incorrect for my needs.
Now that we've established that the desired outcome is indeed a gray scale image, we can fix the code so that is produces a gray scale TIFF.
Two things needs to be changed, first the color space conversion from RGB to Gray:
private static BufferedImage imageToBlackAndWhite(BufferedImage image) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
return op.filter(image, null);
}
I prefer the ColorConvertOp, as it is the most "correct", and on most platforms uses native code. But any of the other methods you listed should also work. You may also want to consider renaming the method to imageToGrayScale for clarity.
In addition, you need to change the TIFF compression setting, as CCITT T.4 compression can only be used with binary black/white images (it's created for FAX transmissions). I suggest you use the Deflate or LZW compression, or perhaps JPEG, if you can live with a lossy compression. These all work well with grayscale data:
private static void writeParamSetUp(ImageWriteParam writeParam) {
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("Deflate"); // or LZW or JPEG
}
PS: You should also get rid of the writeToByteArrayStream method, as your current code writes the TIFF twice, once uncompressed using ImageIO.write(...), then once compressed using writer.write(...).
PPS: The createImage method can also be simplified a lot, given that the ByteArrayOutputStream already contains a full TIFF.
private static void createImage(ByteArrayOutputStream baos) throws Exception {
File file = new File("../proj/src/image2.tif");
Files.write(file.toPath(), baos.toByteArray(), StandardOpenOption.CREATE);
}
Related
I am trying to merge two images in Java. The two photos must be positioned horizontally (the first to the left of the second).
I think I have problems in the write method of the ImageIO class.
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
public class Merge {
public static void main(String[] args) throws IOException {
String p = "../../Desktop/temp/";
BufferedImage left = ImageIO.read(new File(p+"006.jpg"));
BufferedImage right = ImageIO.read(new File(p+"007.jpg"));
BufferedImage imgClone = new BufferedImage(left.getWidth()+right.getWidth(), left.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D cloneG = imgClone.createGraphics();
cloneG.drawImage(right, 0, 0, null);
cloneG.drawImage(left, left.getWidth(), 0, null);
System.out.println(ImageIO.write(imgClone, "jpg", new File(p+"001.jpg"))); //always false
cloneG.dispose();
}
}
ImageIO.write(imgClone, "jpg", new File(p+"001.jpg")) always returns false, I think there is something wrong here but I can't figure out what.
If I go into debugging I can see the merged photo, but then it won't be saved in the folder.
I think it's because JPEG doesn't support transparency, and you used ARGB as the image buffer type. Removing the "A" worked for me.
class Merge {
public static void main(String[] args) throws IOException {
BufferedImage imgClone = new BufferedImage( 50, 50, BufferedImage.TYPE_INT_RGB);
// returns "true"
System.out.println(ImageIO.write(imgClone, "jpg", File.createTempFile( "Test-", "jpg")));
}
}
well,I modified my code to eliminate other factors:
package com.shangzhu.drt;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by lixiaoming on 2017/6/26.
*/
public class ImageTest2 {
private static void insertImageWithPOI() throws Exception {
XSSFWorkbook wwb = new XSSFWorkbook();
XSSFSheet ws = wwb.createSheet("sheet0");
BufferedImage image = ImageIO.read(new File("D:/poi.png"));
ByteArrayOutputStream baps = new ByteArrayOutputStream();
ImageIO.write(image,"png",baps);
int pictureIdx = wwb.addPicture(baps.toByteArray(), Workbook.PICTURE_TYPE_PNG);
XSSFDrawing drawing = ws.createDrawingPatriarch();
XSSFCreationHelper helper = wwb.getCreationHelper();
XSSFClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture picture = drawing.createPicture(anchor, pictureIdx);
picture.resize();
File excelFile = new File("D:/POI.xlsx");
OutputStream ops = new FileOutputStream(excelFile);
wwb.write(ops);
}
public static void main(String[] args) {
try {
insertImageWithPOI();
} catch (Exception e) {
e.printStackTrace();
}
}
}
below is the picture("D:/poi.png") in the code:
D:/poi.png
I don't think the source code which is dealing image has problems,But I don't know what I missed
I confirm that there is a problem when default column size is used. XSSFPicture.resize needs calculating the column widths in pixels to get the XSSFClientAnchor Col2 and the Dx2. As long as default column size is used, then this calculation seems to be wrong.
A workaround could be defining explicit column sizes before using XSSFPicture.resize. Then the calculation of the column widths in pixels seems to be correct.
In your code:
...
Picture picture = drawing.createPicture(anchor, pictureIdx);
for (int c=0; c<20; c++) ws.setColumnWidth(c, 11*256);
picture.resize();
...
I'm working on a project and the goal is to have all images read with ImageIO. This seems to work for everything except GIF images (which display as a static image of the initial frame). I have seen other answers on Stack Overflow and from a thread on the Oracle forums but most require using Java's File class which I can't access due to the program's SecurityManager. I've been able to break the GIF down into an Image array and edit the metadata, but after stitching everything back together I can only display a single image.
Below is a SSCCE for the program:
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GifRenderer {
public static void main(String[] args) throws Exception {
Image image = null;
byte[] imageByteArray = null;
try {
String location = "http://i.imgur.com/Ejh5gJa.gif";
imageByteArray = createByteArray(location);
// This works, but I'm trying to use ImageIO
//image = Toolkit.getDefaultToolkit().createImage(imageByteArray);
InputStream in = new ByteArrayInputStream(imageByteArray);
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setSize(300, 300);
JLabel label = new JLabel(new ImageIcon(image));
frame.add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Constraint: This method simulates how the image is originally received
private static byte[] createByteArray(String urlString) throws IOException {
URL url = new URL(urlString);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byte[] byteChunk = new byte[4096];
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
}
} catch (IOException e) {
e.printStackTrace ();
} finally {
if (is != null) { is.close(); }
}
return baos.toByteArray();
}
}
Some constraints worth mentioning that might not be clear:
The image is originally received as a byte array
The image should be read by ImageIO
The final result should be an Image object
The File class can't be accessed
Given these constraints is there still a way to use ImageIO to display the GIF the same way Toolkit.getDefaultToolkit().createImage() would display the image?
I am trying to save a resized picture to the user's desktop but not sure how to do that.
Here's my code so far:
mi.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String userhome = System.getProperty("user.home");
fileChooser = new JFileChooser(userhome + "\\Desktop");
fileChooser.setAutoscrolls(true);
switch (fileChooser.showOpenDialog(f)) {
case JFileChooser.APPROVE_OPTION:
BufferedImage img = null;
try {
img = ImageIO.read(fileChooser.getSelectedFile());
} catch (IOException e1) {
e1.printStackTrace();
}
Image dimg = img.getScaledInstance(f.getWidth(),
f.getHeight(), Image.SCALE_SMOOTH);
path = new ImageIcon(dimg);
configProps.setProperty("Path", fileChooser
.getSelectedFile().getPath());
imBg.setIcon(path);
break;
}
}
});
The code above resizes the imaged selected to fit the size of the JFrame then sets it to the JLabel.
This all works well but I also want to output the file to a set location lets say to the users desktop to make it easier. I'm currently looking at output stream but can't quite get my head around it.
Any help would be great.
Get the current Icon from the JLabel...
Icon icon = imgBg.getIcon();
Paint the icon to a BufferedImage...
BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
Save the image to a file...
ImageIO.write(img, "png", new File("ResizedIcon.png"));
(and yes, you could use a JFileChooser to pick the file location/name)
You should also take a look at this for better examples of scaling an image, this way, you could scale the BufferedImage to another BufferedImage and save the hassle of having to re-paint the Icon
You might also like to take a look at Writing/Saving an Image
This is a example which is about saving images from Web to the local.
package cn.test.net;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageRequest {
/**
* #param args
*/
public static void main(String[] args) throws Exception {
//a url from web
URL url = new URL("http://img.hexun.com/2011-06-21/130726386.jpg");
//open
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//"GET"!
conn.setRequestMethod("GET");
//Timeout
conn.setConnectTimeout(5 * 1000);
//get data by InputStream
InputStream inStream = conn.getInputStream();
//to the binary , to save
byte[] data = readInputStream(inStream);
//a file to save the image
File imageFile = new File("BeautyGirl.jpg");
FileOutputStream outStream = new FileOutputStream(imageFile);
//write into it
outStream.write(data);
//close the Stream
outStream.close();
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
//every time read length,if -1 ,end
int len = 0;
//a Stream read from buffer
while( (len=inStream.read(buffer)) != -1 ){
//mid parameter for starting position
outStream.write(buffer, 0, len);
}
inStream.close();
//return data
return outStream.toByteArray();
}
}
Hope this is helpful to you!
I have been playing with some of the imaging functionality in Java, trying to superimpose one image over another. Like so:
BufferedImage background = javax.imageio.ImageIO.read(
new ByteArrayInputStream(getDataFromUrl(
"https://www.google.com/intl/en_ALL/images/logo.gif"
))
);
BufferedImage foreground = javax.imageio.ImageIO.read(
new ByteArrayInputStream(getDataFromUrl(
"https://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif"
))
);
WritableRaster backgroundRaster = background.getRaster();
Raster foregroundRaster = foreground.getRaster();
backgroundRaster.setRect(foregroundRaster);
Basically, I was attempting to superimpose this: https://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif
on this: https://www.google.com/intl/en_ALL/images/logo.gif
The product appears as: http://imgur.com/xnpfp.png
From the examples I have seen, this seems to be the appropriate method. Am I missing a step? Is there a better way to handle this? Thank you for your responses.
Seems I was going about this in all the wrong ways. This solution outlined on the Sun forums works perfectly (copied here):
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
class TwoBecomeOne {
public static void main(String[] args) throws IOException {
BufferedImage large = ImageIO.read(new File("images/tiger.jpg"));
BufferedImage small = ImageIO.read(new File("images/bclynx.jpg"));
int w = large.getWidth();
int h = large.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.drawImage(large, 0, 0, null);
g2.drawImage(small, 10, 10, null);
g2.dispose();
ImageIO.write(image, "jpg", new File("twoInOne.jpg"));
JOptionPane.showMessageDialog(null, new ImageIcon(image), "",
JOptionPane.PLAIN_MESSAGE);
}
}