2013-08-01 13 views
12

Sto lavorando a un'applicazione in cui è necessario acquisire un'immagine dalla videocamera. Dopo l'acquisizione, devo convertire Bitmap in Base64. Dopo la conversione in Base64, devo inviare quella stringa al SERVER. Sto usando il codice qui sotto per questo compito:Non è possibile convertire Bitmap in una stringa Base64 perfetta in Android?

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
image.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray(); 
base64Image = Base64.encodeToString(b,Base64.DEFAULT); 

Problema: Quando converto che Base64 all'immagine, sto ottenendo IMMAGINE incompleto. Lo stesso risultato sta accadendo sul server in cui la mia immagine non è perfettamente ricostruita da Base64 String.

Si prega di suggerire la soluzione. Ho già cercato un sacco e ho ottenuto lo stesso codice che sto usando in questo momento.

Modificato: si veda la sottostante immagine incompleto utilizzo Codice

enter image description here

per catturare l'immagine:

intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
startActivityForResult(intent, TAKE_PHOTO); 
+0

si ottiene * IMMAGINE INCOMPLETA * come cosa? –

+0

è il problema che si verifica per tutte le immagini o su uno specifico? –

+0

Dalla stringa base64, viene generato solo il 25% dell'immagine ... e questo sta accadendo con tutte le immagini –

risposta

3

Crea il tuo Base64 classe e di utilizzare questo:

public class Base64 
{ 

    private Base64() 
    { 
    super(); 
    } 

    /** 
    * Encode some data and return a String. 
    */ 
    public final static String encode(byte[] d) 
    { 
    if (d == null) return null; 
    byte data[] = new byte[d.length+2]; 
    System.arraycopy(d, 0, data, 0, d.length); 
    byte dest[] = new byte[(data.length/3)*4]; 

    // 3-byte to 4-byte conversion 
    for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4) 
    { 
     dest[didx] = (byte) ((data[sidx] >>> 2) & 077); 
     dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 | 
        (data[sidx] << 4) & 077); 
     dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 | 
        (data[sidx+1] << 2) & 077); 
     dest[didx+3] = (byte) (data[sidx+2] & 077); 
    } 

    // 0-63 to ascii printable conversion 
    for (int idx = 0; idx <dest.length; idx++) 
    { 
     if (dest[idx] < 26)  dest[idx] = (byte)(dest[idx] + 'A'); 
     else if (dest[idx] < 52) dest[idx] = (byte)(dest[idx] + 'a' - 26); 
     else if (dest[idx] < 62) dest[idx] = (byte)(dest[idx] + '0' - 52); 
     else if (dest[idx] < 63) dest[idx] = (byte)'+'; 
     else   dest[idx] = (byte)'/'; 
    } 

    // add padding 
    for (int idx = dest.length-1; idx > (d.length*4)/3; idx--) 
    { 
     dest[idx] = (byte)'='; 
    } 
    return new String(dest); 
    } 

    /** 
    * Encode a String using Base64 using the default platform encoding 
    **/ 
    public final static String encode(String s) { 
    return encode(s.getBytes()); 
    } 

    /** 
    * Decode data and return bytes. 
    */ 
    public final static byte[] decode(String str) 
    { 
    if (str == null) return null; 
    byte data[] = str.getBytes(); 
    return decode(data); 
    } 

    /** 
    * Decode data and return bytes. Assumes that the data passed 
    * in is ASCII text. 
    */ 
    public final static byte[] decode(byte[] data) 
    { 
    int tail = data.length; 
    while (data[tail-1] == '=') tail--; 
    byte dest[] = new byte[tail - data.length/4]; 

    // ascii printable to 0-63 conversion 
    for (int idx = 0; idx <data.length; idx++) 
    { 
     if (data[idx] == '=') data[idx] = 0; 
     else if (data[idx] == '/') data[idx] = 63; 
     else if (data[idx] == '+') data[idx] = 62; 
     else if (data[idx] >= '0' && data[idx] <= '9') 
     data[idx] = (byte)(data[idx] - ('0' - 52)); 
     else if (data[idx] >= 'a' && data[idx] <= 'z') 
     data[idx] = (byte)(data[idx] - ('a' - 26)); 
     else if (data[idx] >= 'A' && data[idx] <= 'Z') 
     data[idx] = (byte)(data[idx] - 'A'); 
    } 

    // 4-byte to 3-byte conversion 
    int sidx, didx; 
    for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3) 
    { 
     dest[didx] = (byte) (((data[sidx] << 2) & 255) | 
       ((data[sidx+1] >>> 4) & 3)); 
     dest[didx+1] = (byte) (((data[sidx+1] << 4) & 255) | 
       ((data[sidx+2] >>> 2) & 017)); 
     dest[didx+2] = (byte) (((data[sidx+2] << 6) & 255) | 
       (data[sidx+3] & 077)); 
    } 
    if (didx < dest.length) 
    { 
     dest[didx] = (byte) (((data[sidx] << 2) & 255) | 
       ((data[sidx+1] >>> 4) & 3)); 
    } 
    if (++didx < dest.length) 
    { 
     dest[didx] = (byte) (((data[sidx+1] << 4) & 255) | 
       ((data[sidx+2] >>> 2) & 017)); 
    } 
    return dest; 
    } 

    /** 
    * A simple test that encodes and decodes the first commandline argument. 
    */ 
    public static final void main(String[] args) 
    { 
    if (args.length != 1) 
    { 
     System.out.println("Usage: Base64 string"); 
     System.exit(0); 
    } 
    try 
    { 
     String e = Base64.encode(args[0].getBytes()); 
     String d = new String(Base64.decode(e)); 
     System.out.println("Input = '" + args[0] + "'"); 
     System.out.println("Encoded = '" + e + "'"); 
     System.out.println("Decoded = '" + d + "'"); 
    } 
    catch (Exception x) 
    { 
     x.printStackTrace(); 
    } 
    } 
} 

prima convertire la bitmap a ByteArray dal seguente codice:

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
image.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray(); 

Per utilizzare questo byte passaggio chiamata [] dell'immagine bitmap:

base64Image = Base64.encode(b)); 

Si può controllare premendo seguente riga nel proprio browser, vedrai l'immagine

data:image/jpeg;base64,base64Image 
+0

Sì ...Ho provato il tuo codice ... e sopra l'immagine che ho postato proviene solo da questo codice .... Sto ottenendo gli stessi risultati –

0

Quando converto che Base64 per l'immagine, io sono sempre IMMAGINE INCOMPLETA

provare a fare questo con la vostra immagine Bitmap e verificare se qualcosa non è come previsto:

Bitmap originalBitmap = (Bitmap) data.getExtras().get("data"); //or whatever image you want 
Log.d(TAG, "original bitmap byte count: " + originalBitmap.getByteCount()); 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
originalBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
Log.d(TAG, "byte array output stream size: " + baos.size()); 

byte[] outputByteArray = baos.toByteArray(); 
Log.d(TAG, "output byte array length: " + outputByteArray.length); 

String base64EncodedString = Base64.encodeToString(outputByteArray, Base64.DEFAULT); 
Log.d(TAG, "base64 encoded string length: " + base64EncodedString.length()); 

byte[] inputByteArray = Base64.decode(base64EncodedString, Base64.DEFAULT); 
Log.d(TAG, "input byte array length: " + inputByteArray.length); 

ByteArrayInputStream bais = new ByteArrayInputStream(inputByteArray); 
Log.d(TAG, "byte array input stream size: " + bais.available()); 

Bitmap decodedBitmap = BitmapFactory.decodeStream(bais); 
Log.d(TAG, "decoded bitmap byte count: " + decodedBitmap.getByteCount()); 

Log.d(TAG, "decoded bitmap same as original bitmap? " + decodedBitmap.sameAs(originalBitmap)); 

Se tutto va bene, il problema non è la codifica base64. Fammi sapere!

0

Ok, l'ho risolto. il problema era nella memorizzazione della stringa base64 sul database. la mia colonna è stata dichiarata come "TEXT" e taglia le altre parti dell'immagine per la lunghezza della stringa. quindi lo cambio in "LONGTEXT" e ora funziona perfettamente!

Problemi correlati