I have a png file and tried to convert it to jpeg. But the resulting image has wrong colors with very big areas of pink. This is my code:
BufferedImage image = null
BufferedImage imageRGB = null
image = ImageIO.read(new ByteArrayInputStream(imageBytesPng))
imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB)
imageRGB.setData(image.getData())
ByteArrayOutputStream baos=new ByteArrayOutputStream()
ImageIO.write(imageRGB, "jpeg", baos)
baos.flush()
def outImage = baos.toByteArray()
baos.close()
return outImage
What can I change to make the image colors apear as in the png file?
InputStream pngInputStream = ...
OutputStream jpgOutputStream = ...
BufferedImage image = ImageIO.read(pngInputStream));
ImageIO.write(image, "jpeg", jpgOutputStream);
Try this all the best..
import javax.media.jai.*;
public class jai_png_jpg
{
public static void main(String[] args)throws Exception
{
String filename="input_png.png";
//Read input PNG as a PlanarImage file
PlanarImage inputfile = JAI.create("fileload", filename);
//write output in JPG Format
JAI.create("filestore",inputfile,"jai_jpg_output.jpg","JPEG"); } }
Related
we are getting the exception while reading the image,but i have an image in location path.Please suggest us?
byte photoContentByte[] = null;
BufferedImage originalImage = ImageIO.read(new File("D:/xyz/Repository/1234567890.tif"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpeg", baos);
baos.flush();
photoContentByte = baos.toByteArray();
baos.close();
Try this:
BufferedImage originalImage = ImageIO.read(new FileInputStream("D:/xyz/Repository/1234567890.tif"));
EDIT:
As resolved in comments, you have a typo in file extension. It should be
BufferedImage originalImage = ImageIO.read(new File("D:/xyz/Repository/1234567890.tiff"));
i.e., the file extension needs to be tiff and not tif
I am trying to convert a 8 bit gray scale image byte array to jpg image format in java.
static byte[] bytes = new byte[]{126, 126, 127, -128};
public static void main(String[] args) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);
File imageFile = new File("C:\\newrose3.jpg");
ImageIO.write(bufferedImage, "jpg", imageFile);
System.out.println(imageFile.getPath());
}
I have bytes having image data and I want to convert it into readable image format in java.
Assuming bytes is supposed to be pixel data, you should create an image from those bytes, then write it out as JPEG.
Something like:
// Create an image type grayscale
BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
// Get the backing pixels, and copy into it
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(bytes, 0, data, 0, bytes.length);
// Write it out:
ImageIO.write(image, "jpg", new File("yourPathHere");
I'm designing a program that stores geography data on the JavaFX platform, and whenever I convert an image from a JavaFX Image into a BufferedImage then a ByteArray (for the purpose of serialization) before converting to a Buffered Image and then JavaFX Image again, it gets slightly corrupt. Here's the code I'm using to convert back and forth:
private byte [] loadImageData (Image image){
try{
//creating a byte array output stream from the Image
BufferedImage bi = SwingFXUtils.fromFXImage(image, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
ImageIO.write(bi, "png", baos );
baos.flush();
byte[] imageData = baos.toByteArray();
baos.close();
return imageData;
}catch (Exception e){
e.printStackTrace();
}
}
public Image restoreMapData (byte[] data){
try{
//converting back to an image
InputStream in = new ByteArrayInputStream(data);
BufferedImage bi = ImageIO.read(in);
return SwingFXUtils.toFXImage(bi, null);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
Could there be an error elsewhere? I've attached a corrupted and un-corrupt picture of the data.
I also noticed that if I convert to BufferedImage with a type TYPE_INT_ARGB it greatly diminishes the effect.
I need to convert a PNG-File into a CMYK JPEG.
During my research i've found multiple articles on SO decribing that problem. I've copied this answer using BufferedImage and ColorConvertOp.
I came up with this little example:
public static void main(final String[] args) throws IOException
{
final String imageFile = "/tmp/page0.png";
final BufferedImage pngImage = ImageIO.read(new File(imageFile));
// convert PNG to JPEG
// http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/
final BufferedImage rgbImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_INT_RGB);
rgbImage.createGraphics().drawImage(pngImage, 0, 0, Color.WHITE, null);
// RGB to CMYK using ColorConvertOp
// https://stackoverflow.com/questions/380678/how-to-set-icc-color-profile-in-java-and-change-colorspace/2804370#2804370
final ICC_Profile ip = ICC_Profile.getInstance("icc/ISOcoated_v2_300_eci.icc");
// final ICC_Profile ip = ICC_Profile.getInstance("icc/CoatedFOGRA27.icc");
// final ICC_Profile ip = ICC_Profile.getInstance("icc/USWebUncoated.icc");
final ColorConvertOp cco = new ColorConvertOp(new ICC_ColorSpace(ip), null);
final BufferedImage cmykImage = cco.filter(rgbImage, null);
// Write the result into an bytearray
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(cmykImage, "jpg", baos);
baos.flush();
final byte[] imageInByte = baos.toByteArray();
}
The problem is, that it leads me into this exception:
Exception in thread "main" javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1058)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:360)
at javax.imageio.ImageWriter.write(ImageWriter.java:615)
at javax.imageio.ImageIO.doWrite(ImageIO.java:1612)
at javax.imageio.ImageIO.write(ImageIO.java:1578)
at ... .pdf.ReportGeneratorPublicContentTest.main(ReportGeneratorPublicContentTest.java:69)
The message of the Exception doesn't help me. On this thread they say that sun jdk or JAI will fix the problem.
I tried apt-get install libjai-core-java and the oracle JDK jdk1.7.0_51. The error still persists.
#Christian Schneider :
After i download your image file with link of CMYK JPEG, i open file's property. I see color space of image is still RGB. This picture isn't converted to CMYK color.
Please see the below link :
how can I convert an RGB image to CMYK and vice versa in Java?
lovelywib 's answer solved this.
The problem was solved by using TYPE_3BYTE_BGR instead of TYPE_INT_RGB.
public static void main(String[] args) throws Exception
{
final String imageFile = "/tmp/page0.png";
final BufferedImage pngImage = ImageIO.read(new File(imageFile));
// convert PNG to JPEG
// http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/
final BufferedImage rgbImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
rgbImage.createGraphics().drawImage(pngImage, 0, 0, Color.WHITE, null);
// RGB to CMYK using ColorConvertOp
// http://stackoverflow.com/questions/380678/how-to-set-icc-color-profile-in-java-and-change-colorspace/2804370#2804370
final ICC_Profile ip = ICC_Profile.getInstance("icc/USWebUncoated.icc");
final ColorConvertOp cco = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), new ICC_ColorSpace(ip), null);
final BufferedImage cmykImage = new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
cco.filter(rgbImage, cmykImage);
// Write the result into an bytearray
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(cmykImage, "JPEG", baos);
baos.flush();
}
RGB PNG:
https://raw.github.com/d0x/questions/master/stackoverflowPlayground/src/main/resources/so22298328/page0.png
CMYK JPEG: https://raw.github.com/d0x/questions/master/stackoverflowPlayground/src/main/resources/so22298328/page0.cmyk.jpg
Maven Code at GitHub:
https://github.com/d0x/questions/blob/master/stackoverflowPlayground/src/main/java/so22298328/Main.java
I have to convert image URL into an image. For that I tried following coding for convert base64 to an image. While on debugging the code "Bufferedimage image" is always null after ByteArrayInputStream bis=new ByteArrayInputStream(imagebyte). What can I do?
String imageStr = request.getParameter("imgURL");
BufferedImage image = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(imageStr);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
File outputfile = new File("E:\\saved.png");
ImageIO.write(image, "png", outputfile);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
I have to convert image URL into an image.
If that is your only requirement, then will this do?
URL url = new URL("www.example.com/image.png");
BufferedImage image = ImageIO.read(url);
File outputfile = new File("E:\\saved.png");
ImageIO.write(image, "png", outputfile);