Esta publicación muestra dos formas diferentes de convertir una imagen en una matriz de bytes y convertir una matriz de bytes en una imagen.
En primer lugar, el tipo de byte en Java es un entero de complemento a dos con signo de 8 bits. Su rango es [-128, 127]. Una matriz de bytes es solo una matriz de bytes. Una imagen es esencialmente un archivo. Entonces, la tarea es convertir el archivo en una matriz para que pueda almacenarse o transferirse más fácilmente en diferentes tipos de aplicaciones.
1. Método 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. Método 2
La siguiente es una versión más simple que hace lo mismo. Utiliza la clase BufferedImage, que es una forma probada más eficiente.
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. Enlaces de descarga
Puede descargar el código del método 1 y el método 2.