2009-07-06 12 views
48

Ich habe ein Array von Ganzzahlen, die ein RGB-Bild darstellen und es in ein Byte-Array konvertieren und in eine Datei speichern möchten.Wie int [] in Byte [] konvertiert werden

Was ist der beste Weg, um ein Array von ganzen Zahlen in das Array von Bytes in Java zu konvertieren?

+0

Vielleicht werden Sie sich besser und spitze Antworten bekommen, wenn Sie Gründe nennen, warum Sie int [] konvertieren wählen [] auf Byte. –

Antwort

60

Wie Brian sagt, müssen Sie herausfinden, wie welche Art von Konvertierung Sie benötigen.

Möchten Sie es als "normale" Bilddatei (jpg, png usw.) speichern?

Wenn ja, sollten Sie wahrscheinlich die API verwenden.

Wenn Sie es im "Raw" -Format speichern möchten, muss die Reihenfolge angegeben werden, in der die Bytes geschrieben werden sollen. Verwenden Sie dann IntBuffer und NIO.

Als ein Beispiel der Verwendung eines ByteBuffer/IntBuffer Kombination:

import java.nio.*; 
import java.net.*; 

class Test 
{ 
    public static void main(String [] args) 
     throws Exception // Just for simplicity! 
    { 
     int[] data = { 100, 200, 300, 400 }; 

     ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);   
     IntBuffer intBuffer = byteBuffer.asIntBuffer(); 
     intBuffer.put(data); 

     byte[] array = byteBuffer.array(); 

     for (int i=0; i < array.length; i++) 
     { 
      System.out.println(i + ": " + array[i]); 
     } 
    } 
} 
3

Sie müssen entscheiden, wie Sie zuerst eine ganze Zahl in eine Menge von Bytes konvertieren.

Höchstwahrscheinlich (?) 1 ganze Zahl bis 4 Bytes, und verwenden Sie die Shift (>> oder <<) -Operatoren, um jedes Byte zu bekommen (achten Sie auf die Byte-Reihenfolge!). Kopieren Sie in ein Byte-Array 4 mal die Länge des Integer-Arrays.

8

Vielleicht diese Methode verwenden

byte[] integersToBytes(int[] values) 
{ 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    DataOutputStream dos = new DataOutputStream(baos); 
    for(int i=0; i < values.length; ++i) 
    { 
     dos.writeInt(values[i]); 
    } 

    return baos.toByteArray(); 
} 
+0

Dies wird int in Byte aufgeteilt. Ich denke nicht, dass dies in Not ist. –

+0

er möchte es in einer Datei für die spätere Verwendung speichern, so dass dies geeignet wäre – Ram

+0

Warum wickeln Sie 'baos' in' dos' ein? – Supuhstar

2

wenn Ihre Absicht ist, dass Sie vielleicht speichern möchten Datei direkt in einer Datei speichern mit FileOutputStream.write:

OutputStream os = new FileOutputStream("aa"); 
    int[] rgb = { 0xff, 0xff, 0xff }; 
    for (int c : rgb) { 
     os.write(c); 
    } 
    os.close(); 

seit dem:

Schreibt das angegebene Byte in diesen Ausgabestream. Der allgemeine Vertrag für das Schreiben besteht darin, dass ein Byte in den Ausgabestrom geschrieben wird. Das zu schreibende Byte sind die acht niederwertigen Bits des Arguments b. Die 24 höherwertigen Bits von b werden ignoriert.

1

habe ich diesen Code und es funktioniert ziemlich gut:

int IntToByte(byte arrayDst[], int arrayOrg[], int maxOrg){ 
     int i; 
     int idxDst; 
     int maxDst; 
     // 
     maxDst = maxOrg*4; 
     // 
     if (arrayDst==null) 
      return 0; 
     if (arrayOrg==null) 
      return 0; 
     if (arrayDst.length < maxDst) 
      return 0; 
     if (arrayOrg.length < maxOrg) 
      return 0; 
     // 
     idxDst = 0; 
     for (i=0; i<maxOrg; i++){ 
      // Copia o int, byte a byte. 
      arrayDst[idxDst] = (byte)(arrayOrg[i]); 
      idxDst++; 
      arrayDst[idxDst] = (byte)(arrayOrg[i] >> 8); 
      idxDst++; 
      arrayDst[idxDst] = (byte)(arrayOrg[i] >> 16); 
      idxDst++; 
      arrayDst[idxDst] = (byte)(arrayOrg[i] >> 24); 
      idxDst++; 
     } 
     // 
     return idxDst; 
    } 

    int ByteToInt(int arrayDst[], byte arrayOrg[], int maxOrg){ 
     int i; 
     int v; 
     int idxOrg; 
     int maxDst; 
     // 
     maxDst = maxOrg/4; 
     // 
     if (arrayDst==null) 
      return 0; 
     if (arrayOrg==null) 
      return 0; 
     if (arrayDst.length < maxDst) 
      return 0; 
     if (arrayOrg.length < maxOrg) 
      return 0; 
     // 
     idxOrg = 0; 
     for (i=0; i<maxDst; i++){ 
      arrayDst[i] = 0; 
      // 
      v = 0x000000FF & arrayOrg[idxOrg]; 
      arrayDst[i] = arrayDst[i] | v; 
      idxOrg++; 
      // 
      v = 0x000000FF & arrayOrg[idxOrg]; 
      arrayDst[i] = arrayDst[i] | (v << 8); 
      idxOrg++; 
      // 
      v = 0x000000FF & arrayOrg[idxOrg]; 
      arrayDst[i] = arrayDst[i] | (v << 16); 
      idxOrg++; 
      // 
      v = 0x000000FF & arrayOrg[idxOrg]; 
      arrayDst[i] = arrayDst[i] | (v << 24); 
      idxOrg++; 
     } 
     // 
     return maxDst; 
    } 
0

Ich würde 'Dataoutputstream' mit 'ByteArrayOutputStream' verwenden.

public final class Converter { 

    private static final int BYTES_IN_INT = 4; 

    private Converter() {} 

    public static byte [] convert(int [] array) { 
     if (isEmpty(array)) { 
      return new byte[0]; 
     } 

     return writeInts(array); 
    } 

    public static int [] convert(byte [] array) { 
     if (isEmpty(array)) { 
      return new int[0]; 
     } 

     return readInts(array); 
    } 

    private static byte [] writeInts(int [] array) { 
     try { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(array.length * 4); 
      DataOutputStream dos = new DataOutputStream(bos); 
      for (int i = 0; i < array.length; i++) { 
       dos.writeInt(array[i]); 
      } 

      return bos.toByteArray(); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    private static int [] readInts(byte [] array) { 
     try { 
      ByteArrayInputStream bis = new ByteArrayInputStream(array); 
      DataInputStream dataInputStream = new DataInputStream(bis); 
      int size = array.length/BYTES_IN_INT; 
      int[] res = new int[size]; 
      for (int i = 0; i < size; i++) { 
       res[i] = dataInputStream.readInt(); 
      } 
      return res; 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 

    public class ConverterTest { 

    @Test 
    public void convert() { 
     final int [] array = {-1000000, 24000, -1, 40}; 
     byte [] bytes = Converter.convert(array); 
     int [] array2 = Converter.convert(bytes); 

     assertTrue(ArrayUtils.equals(array, array2)); 

     System.out.println(Arrays.toString(array)); 
     System.out.println(Arrays.toString(bytes)); 
     System.out.println(Arrays.toString(array2)); 
    } 
} 

Drucke:

[-1000000, 24000, -1, 40] 
[-1, -16, -67, -64, 0, 0, 93, -64, -1, -1, -1, -1, 0, 0, 0, 40] 
[-1000000, 24000, -1, 40] 
+2

Das ist fast genau das gleiche wie in dieser Antwort: https://StackOverflow.com/a/1086071 (der einzige Unterschied, den ich sehe, ist Ihr Anfangswert für den 'ByteArrayOutputStream'). Kannst du erklären, wie diese Antwort neue Informationen hinzufügt? – Tom

+0

Einfach Refactoring. –