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
Related
I have an image stored as a byte array. Regardless how large the size, I want to always resize it down to 100x100 pixels.
I am trying to convert the byte array into a bufferedimage, resize and save it back as a bytearray. But with the following code, the images no longer appear on my page.
byte[] pict; // this array contains the image
BufferedImage bufferedImage;
ByteArrayInputStream bais = new ByteArrayInputStream(pict);
try {
bufferedImage = ImageIO.read(bais);
bufferedImage = Scalr.resize(bufferedImage, 100);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, "jpg", baos );
baos.flush();
pict = baos.toByteArray();
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream o = response.getOutputStream();
o.write(pict);
Do it this way instead:
Image tmpImage = ImageIO.read(bais);
Image scaled = tmpImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
Then, to convert to a byte array, convert the image into a bufferedimage and then that into the byte array:
BufferedImage buffered = ((ToolkitImage) scaled).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "jpg", baos);
baos.flush();
pict = baos.toByteArray();
baos.close();
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);
I need to obtain the pixel data from a BufferedImage so I can recreate the image from the data. I looked into Raster, but that did not seem to contain the information I need. How can I obtain data from a BufferedImage so I can recreate the image without needing the original file?
You should check out the answers to this question
Java - get pixel array from image
One way to do it is to use
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
byte[] imageBytes = baos.toByteArray();
baos.close();
Now when you want to create a new BufferedImage from the data you use
ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
BufferedImage newImage = null;
try {
newImage = ImageIO.read(bais);
} catch (IOException e) {
// handle exception
}
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"); } }
I have a java.awt.Image that I need to add CCITT T.6 compression and convert to a TIFF byte array. I have seen some examples of using TIFFImageWriteParam and other classes from the javax.imageio package but I can’t find a complete example going all the way from Image to byte array.
Here is what I have so far beginning with a java.awt.Image obtained from scanning. This works just fine to generate a byte array of a TIFF, but I need to find a way, using TIFFImageWriteParam or some other means, to compress the TIFF prior to processing it as a byte array:
thisImage = ... a java.awt.Image from a scanner
if(thisImage!=null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage bimg = new BufferedImage(thisImage.getWidth(null),thisImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
bimg.createGraphics().drawImage(thisImage, 0, 0, null);
try {
ImageIO.write(bimg, "tiff", baos);
} catch (Exception e) {
e.printStackTrace();
}
thisByteArray = baos.toByteArray();
...
Any help would be appreciated.
I found a solution thanks to: this thread.
Here is what I ended up doing that solved my issue:
thisImage = thisImage = ... a java.awt.Image from a scanner
if(thisImage!=null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
boolean foundWriter = false;
BufferedImage bimg = new BufferedImage(thisImage.getWidth(null),thisImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
bimg.createGraphics().drawImage(thisImage, 0, 0, null);
for(Iterator<ImageWriter> writerIter = ImageIO.getImageWritersByFormatName("tif"); writerIter.hasNext() && !foundWriter;) {
foundWriter = true;
ImageWriter writer = (ImageWriter)writerIter.next();
writer.setOutput(ios);
TIFFImageWriteParam writeParam = (TIFFImageWriteParam)writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("CCITT T.6");
writer.prepareWriteSequence(null);
ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(bimg);
javax.imageio.metadata.IIOMetadata metadata = writer.getDefaultImageMetadata(spec, writeParam);
IIOImage iioImage = new IIOImage(bimg, null, metadata);
writer.writeToSequence(iioImage, writeParam);
bimg.flush();
writer.endWriteSequence();
ios.flush();
writer.dispose();
ios.close();
thisByteArray = baos.toByteArray();
baos.close();
}
}