ciao a tutti!!!
ho un codice che mi invia tramite FileInputStream una immagine (dandogli l'indirizzo dell'immagine) e poi la riceve e la fa apparire a video. Per inviarla la spezzetta in un array di byte e invia quest'ultimo. Il mio problema è che voglio inviare direttamente l'array di bytes, ovvero non voglio passargli l'indirizzo dell'immagine ma voglio passargli direttamente l'array di bytes. E' possibile farlo?
per prendere l'array di bytes dell'immagine ho provato ad aprirla con un editor di testo esadecimale e ho ottenuto questo:

FF D8 FF E0 00 10 4A 46 49 46 00 01 02 01 00 48 00 48 00 00 FF E1 03 B6 45 78 69 66 00 00 4D 4D 00 2A 00 00 00 08 00 07 01 12 00 03 00 00 00 01 00 01 00 00 01 1A 00 05 00 00 00 01 00 00 00 62 01 1B

ovviamente questo è solo una piccola parte... per avere l'array di byte dell'immagine posso procedere diversamente?

per creare l'array di byte in java, si fa così?

byte[] bytes = new byte[] {(byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0, (byte)0x00, (byte)0x10, (byte)0x4A, (byte)0x46, (byte)0x49, (byte)0x46, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x48, (byte)0x00};

oppure è sbagliato?

scusate le mille domande ma sono bloccato in questo punto... GRAZIE A TUTTI



questo è il codice che mi fa l'invio dell'immagine (ho allegato anche il file):

codice:



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 {
    	/*
    	 * In this function the first part shows how to convert an image file to 
    	 * byte array. The second part of the code shows how to change byte array
    	 * back to a image
    	 */

   int i;
        File file = new File("strawberry.jpg");
        System.out.println(file.exists() + "!!");
 
        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.
 
        //InputStream in = resource.openStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
         // è tutto a 0!!!!
        
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); 
                //no doubt here is 0
                /*Writes len bytes from the specified byte array starting at offset 
                off to this byte array output stream.*/
                System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
        }
 
      byte[] bytes = bos.toByteArray();

        //bytes is the ByteArray we need
//byte[] bytes = new byte[] {(byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0, (byte)0x00, (byte)0x10, (byte)0x4A, (byte)0x46, (byte)0x49, (byte)0x46, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x48, (byte)0x00, (byte)0x48, (byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xE1, (byte)0x03, (byte)0xB6, (byte)0x45, (byte)0x78, (byte)0x69, (byte)0x66, (byte)0x00, (byte)0x00, (byte)0x4D, (byte)0x4D, (byte)0x00, (byte)0x2A, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x07, (byte)0x01, (byte)0x12, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x1A, (byte)0x00, (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x62, (byte)0x01, (byte)0x1B, (byte)0x00, (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x6A, (byte)0x01, (byte)0x28, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x31, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x1B, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x72, (byte)0x01, (byte)0x32, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x14, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x8D, (byte)0x87, (byte)0x69, (byte)0x00, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xA4, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xD0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x48, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x48, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x41, (byte)0x64, (byte)0x6F, (byte)0x62, (byte)0x65, (byte)0x20, (byte)0x50, (byte)0x68, (byte)0x6F, (byte)0x74, (byte)0x6F, (byte)0x73, (byte)0x68, (byte)0x6F, (byte)0x70, (byte)0x20, (byte)0x43, (byte)0x53, (byte)0x20, (byte)0x57, (byte)0x69, (byte)0x6E, (byte)0x64, (byte)0x6F, (byte)0x77, (byte)0x73, (byte)0x00, (byte)0x32, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x3A, (byte)0x30, (byte)0x36, (byte)0x3A, (byte)0x31, (byte)0x31, (byte)0x20, (byte)0x31, (byte)0x37, (byte)0x3A, (byte)0x34, (byte)0x33, (byte)0x3A, (byte)0x33, (byte)0x37, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0xA0, (byte)0x01, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0xFF, (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0xA0, (byte)0x02, (byte)0x00, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0A, (byte)0xA0, (byte)0x03, (byte)0x00, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00};
 
 
        /*
         * The second part shows how to convert byte array back to an image file  
         */
 for (i=0;i<2;i++)
   System.out.print( bytes[i] );
  System.out.print("\nha finito di far vedere i byte.\n" );
        //Before is how to change ByteArray back to Image
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
        //ImageIO is a class containing static convenience methods for locating ImageReaders
        //and ImageWriters, and performing simple encoding and decoding. 
 
        ImageReader reader = (ImageReader) readers.next();
        Object source = bis; // File or InputStream, it seems file is OK
 
        ImageInputStream iis = ImageIO.createImageInputStream(source);
        //Returns an ImageInputStream that will take its input from the given Object
 
        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("strawberry2.jpg");
        ImageIO.write(bufferedImage, "jpg", imageFile);
        //"jpg" is the format of the image
        //imageFile is the file to be written to.
 
        System.out.println(imageFile.getPath());
    }
}