How to convert an image to an array of bytes in java?

1

I'm trying to convert an image to an array of bytes in java to save it in a database, I used several methods to perform the conversion, and you could say that if it does, but it always returns a small part, this one of the codes that I have occupied:

public byte[] extractBytes(File imgPath) throws IOException {

        BufferedImage bufferedImage = ImageIO.read(imgPath);

        RecortaFotografia recorte = new RecortaFotografia(bufferedImage, 100, 100);

        WritableRaster raster = bufferedImage.getRaster();
        DataBufferByte data = (DataBufferByte) raster.getDataBuffer();

        return (data.getData());
    }

But the result is always something similar to this:

[[email protected]

I have used other methods for conversion and the result is always the same. I do not find my mistake, or what I'm doing wrong. Why do you always return only part of me? or how can I get the entire string of the byte array?

    
asked by Ken 08.10.2016 в 01:11
source

2 answers

2
  

But the result is always something similar to this:

[[email protected]

It is completely normal why you are printing a object . When you print an object - if you have not overwritten the toString method, what you will get is:

[email protected]

These are basic notions. To print your byte array you should do a simple for:

for(byte b : bytes) { System.out.print(b); }

Or lambda style:

Arrays.stream(bytes).forEach(System.out::print);

An easy way to convert an image to byte[] is this:

public static Optional<byte[]> toBinary(String path) {
    int len = path.split("\.").length;
    String ext = path.split("\.")[len - 1];
    try {
        BufferedImage img = ImageIO.read(new File(path));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, ext, baos);
        return Optional.of(baos.toByteArray());
    } catch(IOException e) {
        return Optional.empty();
    }
}

And we would use it like this:

Optional<byte[]> binary = ImageUtils.toBinary("ruta-abs-imagen");
// la imagen se procesó sin problemas y hay datos
if(binary.isPresent()) {
    byte[] image = binary.get();
}
    
answered by 08.10.2016 в 03:17
0

The result is the value of Objeto that your method returns, maybe you get the value as follows

/* Por eso Obtienes ese valor */
System.out.println(extractBytes(new File("urlImage"))); 
  • The first part of this output shows the Object Type where appropriate B that is Byte
  • The second part is the representation hexadecimal of the hashCode () of your object. (172675af)

To obtain the values returned by your método you should receive them before .. a simple way to do this operation would be.

   byte[] retorno = extractBytes(new File("urlfile"));
    /* Imprimir los Bytes */
    for (byte s : retorno) {
        System.out.println(s& 0xff); 
     /* Si quiere hacer sus valores Positivos sino simplemente sout(s) */
    }
  

Another way to print (If you want to do it) would be to use the class    Arrays and its method toString (byte [] array)

    
answered by 08.10.2016 в 03:17