Home > Default > Saving a tiff image N preserviing its resolution

Saving a tiff image N preserviing its resolution

November 30Hits:0
Advertisement
hi guys ,
I am facing a problem saving an existing tiff file to a tiff format.
My original image is of 600 dpi both horizontal and vertical resolution.
When i save using my patch of code the resulting image is 96dpi in resolution.
I need to make sure taht there is no loss when i am saving my image
public void saveImage()
     File file3; // The file that the user wants to save.
JFileChooser fd; // File dialog that lets the user specify the file.
fd = new JFileChooser(".");
fd.setDialogTitle("Save Image As TIFF...");
int action = fd.showSaveDialog(this);
if (action != JFileChooser.APPROVE_OPTION) {
return;
file3 = fd.getSelectedFile();
if (file3.exists()) {
// If file already exists, ask before replacing it.
action = JOptionPane.showConfirmDialog(this,
"Replace existing file?");
if (action != JOptionPane.YES_OPTION)
return;
try {
FileOutputStream os = new FileOutputStream(file3);
TIFFEncodeParam param1 = new TIFFEncodeParam();// USE WHEN CONVERTING TO TIFF FILE
if(factor.isBinary())
     //param1.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); //USE IF BINARY IMAGE
     param1.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
     //param1.setCompression(TIFFEncodeParam.COMPRESSION_NONE);//USE IF BINARY IMAGE
     //param1.setCompression(TIFFEncodeParam.COMPRESSION_LZW); //USE IF BINARY IMAGE
else
     param1.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);
     //param1.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
ImageEncoder enc =ImageCodec.createImageEncoder("TIFF",os,param1);
//enc.encode(source);
enc.encode(backup);
os.flush();
os.close();
catch (IOException e)
               // Some error has occurred while trying to write.
               //Show an error message.
               JOptionPane.showMessageDialog(this,
                              "Sorry, an error has occurred:\n" + e.getMessage());
Any suggestions ?
Aadil

Answers

Hi,
Could you find any method to change the tiff image compression or even resolution.
When i try this,
String filename="c:\\sample1.tif";      
          File file = new File(filename);
     SeekableStream s = new FileSeekableStream(file);
     ImageDecoder dec = ImageCodec.createImageDecoder("TIFF", s, null);
     //RenderedImage op = new NullOpImage(dec.decodeAsRenderedImage(), null,null, OpImage.OP_COMPUTE_BOUND);
     RenderedImage op = new NullOpImage(dec.decodeAsRenderedImage(0),null,null,OpImage.OP_IO_BOUND);
     ByteArrayOutputStream output = new ByteArrayOutputStream();
     TIFFEncodeParam tp=new TIFFEncodeParam();
     tp.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
     //tp.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_2D);
     ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", output, tp);
     encoder.encode(op);
     encoder.setParam(tp);
     System.out.println(tp.getCompression());
     byte compressedData[] = output.toByteArray();
     FileOutputStream fouts=new FileOutputStream("c:\\sample2.tif");
     fouts.write(compressedData);
     output.close();
     fouts.close();
It's giving errro saying that "End of data reached before next EOL encountered" when i call encoder.encode(op);
method..
any suggestions?????

Read other 5 answers

Tags:

Related Articles

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 1.402 s.