Solved

Convert Uncompressed JPEG Image to byte Array in Java

Posted on 2007-03-31
5
6,805 Views
Last Modified: 2008-01-09
May i know how can i convert an uncompressed JPEG image into sequence of byte (i.e Byte Array) and vice versa (i.e convert from Byte Array to Uncompressed JPEG Image) using JAVA.
0
Comment
Question by:Angora
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 18828820
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", out);
byte[] imageBytes = out.toByteArray();

BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imageBytes));
0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 18830296
File jpeg = new File("my.jpg");
byte[] bytes = new byte[jpeg.length()];
DataInputStream in = new DataInputStream(new FileInputStream(jpeg));
in.readFully(bytes);
in.close();

// bytes will now contain contents of jpeg file

// to convert back to a file:

FileOutputStream out = new FileOutputStream("out.jpg");
out.write(bytes);
out.close();
0
 
LVL 92

Expert Comment

by:objects
ID: 18830311
The follpowing examples show good examples of reading/saving *Java* images if you need to use the jpeg as an Image in your application

http://www.exampledepot.com/egs/javax.imageio/BasicImageRead.html
http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question