Java: convert image to byte array, convert byte array to image
This post shows two different ways to convert an image to a byte array and convert a byte array to an image.
First of all, the byte type in Java is an 8-bit signed two's complement integer. Its range is [-128, 127]. A byte array is just an array of bytes. An image is essentially a file. So the task is converting the file to an array so that it can be stored or transferred more easily in different kinds of applications.
1. Method 1
import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; public class ConvertImage { public static void main(String[] args) throws FileNotFoundException, IOException { /* * 1. How to convert an image file to byte array? */ File file = new File("C:\\rose.jpg"); FileInputStream fis = new FileInputStream(file); //create FileInputStream which obtains input bytes from a file in a file system //FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; try { for (int readNum; (readNum = fis.read(buf)) != -1;) { //Writes to this byte array output stream bos.write(buf, 0, readNum); System.out.println("read " + readNum + " bytes,"); } } catch (IOException ex) { Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex); } byte[] bytes = bos.toByteArray(); /* * 2. How to convert byte array back to an image file? */ ByteArrayInputStream bis = new ByteArrayInputStream(bytes); Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg"); //ImageIO is a class containing static methods for locating ImageReaders //and ImageWriters, and performing simple encoding and decoding. 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); //got an image file BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); //bufferedImage is the RenderedImage to be written Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, null, null); File imageFile = new File("C:\\newrose2.jpg"); ImageIO.write(bufferedImage, "jpg", imageFile); System.out.println(imageFile.getPath()); } } |
2. Method 2
The following is a simpler version which does the same thing. It uses the BufferedImage class, which is a proved more efficient way.
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; public class SimpleConvertImage { public static void main(String[] args) throws IOException{ String dirName="C:\\"; ByteArrayOutputStream baos=new ByteArrayOutputStream(1000); BufferedImage img=ImageIO.read(new File(dirName,"rose.jpg")); ImageIO.write(img, "jpg", baos); baos.flush(); String base64String=Base64.encode(baos.toByteArray()); baos.close(); byte[] bytearray = Base64.decode(base64String); BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray)); ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg")); } } |
3. Download Links
You can download the code of method 1 and method 2.
<pre><code> String foo = "bar"; </code></pre>
Pingback: 从MYSQL检索BLOB数据并创建映像时出错 Download()
Pingback: Convert A Web Page/web Creation To Excel | Web Creation Trends()
Pingback: Convert Access Form To Web Page/web Creation | Web Creation Trends()
Pingback: Convert Excel File Into Web Page/web Creation | Web Creation Trends()
Pingback: Convert Access To Web Page/web Creation Form | Web Creation Trends()
Pingback: Convert Web Page/web Creation To Doc File | Web Creation Trends()
Pingback: Convert Web Page/web Creation To Png | Web Creation Trends()
Pingback: Convert Web Page/web Creation Into Image Using Java | Web Creation Trends()
Pingback: How does ImageIO determine the dimension of images?CopyQuery CopyQuery | Question & Answer Tool for your Technical Queries,CopyQuery, ejjuit, query, copyquery, copyquery.com, android doubt, ios question, sql query, sqlite query, nodejsquery, dns query()