Compressing and converting a jpg to tiff in Java












0















I have a jpg image and I want to convert it to a tiff file, but when I create the output file from byteArrayOutputStream, the output file has 0 byte length.



public static void main(String args) throws Exception {
String root = "E:\Temp\imaging\test\";

File image = new File(root + "0riginalTif-convertedToJpg.JPG");

byte bytes = compressJpgToTiff(image);
File destination = new File(root + "OriginalJpg-compressedToTiff.tiff");
FileOutputStream fileOutputStream = new FileOutputStream(destination);
fileOutputStream.write(bytes);
}


public static byte compressJpgToTiff(File imageFile) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(255);
ImageOutputStream imageOutputStream = null;
try {
File input = new File(imageFile.getAbsolutePath());

Iterator<ImageWriter> imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
ImageWriter writer = imageWriterIterator.next();
imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
writer.setOutput(imageOutputStream);

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG");
param.setCompressionQuality(0.1f);

BufferedImage bufferedImage = ImageIO.read(input);
writer.write(null, new IIOImage(bufferedImage, null, null), param);
writer.dispose();
return byteArrayOutputStream.toByteArray();

} catch (Exception e) {

throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}


I want to reduce the size of output tiff as much as possible. Is there a better approach? Is it even possible to reduce the size of a tiff image?










share|improve this question

























  • 1) For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "I want to reduce the size of output tiff as much as possible." Why not save it as a JPEG with higher compression?

    – Andrew Thompson
    Nov 19 '18 at 13:41













  • Because I want it to be tiff!

    – Daniel Smith
    Nov 20 '18 at 4:28











  • So .. where is the MCVE / SSCCE?

    – Andrew Thompson
    Nov 20 '18 at 6:02
















0















I have a jpg image and I want to convert it to a tiff file, but when I create the output file from byteArrayOutputStream, the output file has 0 byte length.



public static void main(String args) throws Exception {
String root = "E:\Temp\imaging\test\";

File image = new File(root + "0riginalTif-convertedToJpg.JPG");

byte bytes = compressJpgToTiff(image);
File destination = new File(root + "OriginalJpg-compressedToTiff.tiff");
FileOutputStream fileOutputStream = new FileOutputStream(destination);
fileOutputStream.write(bytes);
}


public static byte compressJpgToTiff(File imageFile) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(255);
ImageOutputStream imageOutputStream = null;
try {
File input = new File(imageFile.getAbsolutePath());

Iterator<ImageWriter> imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
ImageWriter writer = imageWriterIterator.next();
imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
writer.setOutput(imageOutputStream);

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG");
param.setCompressionQuality(0.1f);

BufferedImage bufferedImage = ImageIO.read(input);
writer.write(null, new IIOImage(bufferedImage, null, null), param);
writer.dispose();
return byteArrayOutputStream.toByteArray();

} catch (Exception e) {

throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}


I want to reduce the size of output tiff as much as possible. Is there a better approach? Is it even possible to reduce the size of a tiff image?










share|improve this question

























  • 1) For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "I want to reduce the size of output tiff as much as possible." Why not save it as a JPEG with higher compression?

    – Andrew Thompson
    Nov 19 '18 at 13:41













  • Because I want it to be tiff!

    – Daniel Smith
    Nov 20 '18 at 4:28











  • So .. where is the MCVE / SSCCE?

    – Andrew Thompson
    Nov 20 '18 at 6:02














0












0








0


0






I have a jpg image and I want to convert it to a tiff file, but when I create the output file from byteArrayOutputStream, the output file has 0 byte length.



public static void main(String args) throws Exception {
String root = "E:\Temp\imaging\test\";

File image = new File(root + "0riginalTif-convertedToJpg.JPG");

byte bytes = compressJpgToTiff(image);
File destination = new File(root + "OriginalJpg-compressedToTiff.tiff");
FileOutputStream fileOutputStream = new FileOutputStream(destination);
fileOutputStream.write(bytes);
}


public static byte compressJpgToTiff(File imageFile) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(255);
ImageOutputStream imageOutputStream = null;
try {
File input = new File(imageFile.getAbsolutePath());

Iterator<ImageWriter> imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
ImageWriter writer = imageWriterIterator.next();
imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
writer.setOutput(imageOutputStream);

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG");
param.setCompressionQuality(0.1f);

BufferedImage bufferedImage = ImageIO.read(input);
writer.write(null, new IIOImage(bufferedImage, null, null), param);
writer.dispose();
return byteArrayOutputStream.toByteArray();

} catch (Exception e) {

throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}


I want to reduce the size of output tiff as much as possible. Is there a better approach? Is it even possible to reduce the size of a tiff image?










share|improve this question
















I have a jpg image and I want to convert it to a tiff file, but when I create the output file from byteArrayOutputStream, the output file has 0 byte length.



public static void main(String args) throws Exception {
String root = "E:\Temp\imaging\test\";

File image = new File(root + "0riginalTif-convertedToJpg.JPG");

byte bytes = compressJpgToTiff(image);
File destination = new File(root + "OriginalJpg-compressedToTiff.tiff");
FileOutputStream fileOutputStream = new FileOutputStream(destination);
fileOutputStream.write(bytes);
}


public static byte compressJpgToTiff(File imageFile) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(255);
ImageOutputStream imageOutputStream = null;
try {
File input = new File(imageFile.getAbsolutePath());

Iterator<ImageWriter> imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
ImageWriter writer = imageWriterIterator.next();
imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
writer.setOutput(imageOutputStream);

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG");
param.setCompressionQuality(0.1f);

BufferedImage bufferedImage = ImageIO.read(input);
writer.write(null, new IIOImage(bufferedImage, null, null), param);
writer.dispose();
return byteArrayOutputStream.toByteArray();

} catch (Exception e) {

throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}


I want to reduce the size of output tiff as much as possible. Is there a better approach? Is it even possible to reduce the size of a tiff image?







java image jpeg tiff






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:41









Andrew Thompson

153k27163339




153k27163339










asked Nov 19 '18 at 11:12









Daniel SmithDaniel Smith

13




13













  • 1) For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "I want to reduce the size of output tiff as much as possible." Why not save it as a JPEG with higher compression?

    – Andrew Thompson
    Nov 19 '18 at 13:41













  • Because I want it to be tiff!

    – Daniel Smith
    Nov 20 '18 at 4:28











  • So .. where is the MCVE / SSCCE?

    – Andrew Thompson
    Nov 20 '18 at 6:02



















  • 1) For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "I want to reduce the size of output tiff as much as possible." Why not save it as a JPEG with higher compression?

    – Andrew Thompson
    Nov 19 '18 at 13:41













  • Because I want it to be tiff!

    – Daniel Smith
    Nov 20 '18 at 4:28











  • So .. where is the MCVE / SSCCE?

    – Andrew Thompson
    Nov 20 '18 at 6:02

















1) For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "I want to reduce the size of output tiff as much as possible." Why not save it as a JPEG with higher compression?

– Andrew Thompson
Nov 19 '18 at 13:41







1) For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) "I want to reduce the size of output tiff as much as possible." Why not save it as a JPEG with higher compression?

– Andrew Thompson
Nov 19 '18 at 13:41















Because I want it to be tiff!

– Daniel Smith
Nov 20 '18 at 4:28





Because I want it to be tiff!

– Daniel Smith
Nov 20 '18 at 4:28













So .. where is the MCVE / SSCCE?

– Andrew Thompson
Nov 20 '18 at 6:02





So .. where is the MCVE / SSCCE?

– Andrew Thompson
Nov 20 '18 at 6:02












2 Answers
2






active

oldest

votes


















0














return byteArrayOutputStream.toByteArray(); but you didn't wirite data to byteArrayOutputStream. Look, you just added data to writer.



About compression of tiff file, you have already done it with - param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);






share|improve this answer
























  • I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

    – Daniel Smith
    Nov 19 '18 at 11:39



















0














Your byteArrayOutputStream object is getting closed in finally block before you converting byteArrayOutputStream to byteArray using byteArrayOutputStream.toByteArray() thats why you getting content length to be 0. So modify your code once as below :



public static byte compressJpgToTiff(File imageFile) throws Exception {
//Add rest of your method code here
writer.dispose();
byte bytesToReturn = byteArrayOutputStream.toByteArray();
return bytesToReturn;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}





share|improve this answer
























  • Thanks for your answer, but obviously that's not the reason.

    – Daniel Smith
    Nov 19 '18 at 12:17











  • In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

    – apandey846
    Nov 19 '18 at 12:27











  • I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

    – Daniel Smith
    Nov 19 '18 at 12:32













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373392%2fcompressing-and-converting-a-jpg-to-tiff-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














return byteArrayOutputStream.toByteArray(); but you didn't wirite data to byteArrayOutputStream. Look, you just added data to writer.



About compression of tiff file, you have already done it with - param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);






share|improve this answer
























  • I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

    – Daniel Smith
    Nov 19 '18 at 11:39
















0














return byteArrayOutputStream.toByteArray(); but you didn't wirite data to byteArrayOutputStream. Look, you just added data to writer.



About compression of tiff file, you have already done it with - param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);






share|improve this answer
























  • I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

    – Daniel Smith
    Nov 19 '18 at 11:39














0












0








0







return byteArrayOutputStream.toByteArray(); but you didn't wirite data to byteArrayOutputStream. Look, you just added data to writer.



About compression of tiff file, you have already done it with - param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);






share|improve this answer













return byteArrayOutputStream.toByteArray(); but you didn't wirite data to byteArrayOutputStream. Look, you just added data to writer.



About compression of tiff file, you have already done it with - param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 11:23









BSeitkazinBSeitkazin

1,5331226




1,5331226













  • I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

    – Daniel Smith
    Nov 19 '18 at 11:39



















  • I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

    – Daniel Smith
    Nov 19 '18 at 11:39

















I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

– Daniel Smith
Nov 19 '18 at 11:39





I did it: imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); writer.setOutput(imageOutputStream);

– Daniel Smith
Nov 19 '18 at 11:39













0














Your byteArrayOutputStream object is getting closed in finally block before you converting byteArrayOutputStream to byteArray using byteArrayOutputStream.toByteArray() thats why you getting content length to be 0. So modify your code once as below :



public static byte compressJpgToTiff(File imageFile) throws Exception {
//Add rest of your method code here
writer.dispose();
byte bytesToReturn = byteArrayOutputStream.toByteArray();
return bytesToReturn;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}





share|improve this answer
























  • Thanks for your answer, but obviously that's not the reason.

    – Daniel Smith
    Nov 19 '18 at 12:17











  • In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

    – apandey846
    Nov 19 '18 at 12:27











  • I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

    – Daniel Smith
    Nov 19 '18 at 12:32


















0














Your byteArrayOutputStream object is getting closed in finally block before you converting byteArrayOutputStream to byteArray using byteArrayOutputStream.toByteArray() thats why you getting content length to be 0. So modify your code once as below :



public static byte compressJpgToTiff(File imageFile) throws Exception {
//Add rest of your method code here
writer.dispose();
byte bytesToReturn = byteArrayOutputStream.toByteArray();
return bytesToReturn;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}





share|improve this answer
























  • Thanks for your answer, but obviously that's not the reason.

    – Daniel Smith
    Nov 19 '18 at 12:17











  • In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

    – apandey846
    Nov 19 '18 at 12:27











  • I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

    – Daniel Smith
    Nov 19 '18 at 12:32
















0












0








0







Your byteArrayOutputStream object is getting closed in finally block before you converting byteArrayOutputStream to byteArray using byteArrayOutputStream.toByteArray() thats why you getting content length to be 0. So modify your code once as below :



public static byte compressJpgToTiff(File imageFile) throws Exception {
//Add rest of your method code here
writer.dispose();
byte bytesToReturn = byteArrayOutputStream.toByteArray();
return bytesToReturn;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}





share|improve this answer













Your byteArrayOutputStream object is getting closed in finally block before you converting byteArrayOutputStream to byteArray using byteArrayOutputStream.toByteArray() thats why you getting content length to be 0. So modify your code once as below :



public static byte compressJpgToTiff(File imageFile) throws Exception {
//Add rest of your method code here
writer.dispose();
byte bytesToReturn = byteArrayOutputStream.toByteArray();
return bytesToReturn;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (imageOutputStream != null)
imageOutputStream.close();
byteArrayOutputStream.close();
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 12:05









apandey846apandey846

8991019




8991019













  • Thanks for your answer, but obviously that's not the reason.

    – Daniel Smith
    Nov 19 '18 at 12:17











  • In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

    – apandey846
    Nov 19 '18 at 12:27











  • I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

    – Daniel Smith
    Nov 19 '18 at 12:32





















  • Thanks for your answer, but obviously that's not the reason.

    – Daniel Smith
    Nov 19 '18 at 12:17











  • In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

    – apandey846
    Nov 19 '18 at 12:27











  • I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

    – Daniel Smith
    Nov 19 '18 at 12:32



















Thanks for your answer, but obviously that's not the reason.

– Daniel Smith
Nov 19 '18 at 12:17





Thanks for your answer, but obviously that's not the reason.

– Daniel Smith
Nov 19 '18 at 12:17













In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

– apandey846
Nov 19 '18 at 12:27





In that case just debug yourself line by line and see where stream is getting lost or reaching EOF. But just by seeing your code first thought came that in finally block which runs before your return statement closing your stream object than returning toByte part which will obviously have nothing to convert to byte. So recheck again.

– apandey846
Nov 19 '18 at 12:27













I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

– Daniel Smith
Nov 19 '18 at 12:32







I already did that and still I cant figure it out. The byteArrayOutputStream is never filled in with data.

– Daniel Smith
Nov 19 '18 at 12:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373392%2fcompressing-and-converting-a-jpg-to-tiff-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents