IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

ByteArray to string


Sujet :

Android

  1. #1
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    26
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 26
    Points : 16
    Points
    16
    Par défaut ByteArray to string
    Bonjour et bonne année,
    Je suis dans une petite impasse, je voudrais convertir un byte Array contenant les informations d'une image prise depuis la camera en string.

    J'arrive a voir l'image si je l'enregistre en image (dans un FileOutputStream) mais si je passe par une ByteArrayOutputStream, non seulement, la string reçu ne correspond pas à ce qui est contenu dans le fichier image mais même la taille mémoire est 2 fois plus grande. Voici le PictureCallback que j'utilise.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
     Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
            public void onPictureTaken(byte[] data, Camera c) {
                Log.e(TAG, "PICTURE CALLBACK: data.length = " + data.length);
               String imageString = null;
     
                FileOutputStream fOut = null; 
                OutputStreamWriter osw = null;
                ByteArrayOutputStream bos = null;
                try{ 
                	bos = new ByteArrayOutputStream() ;
     
                	Bitmap myPic = BitmapFactory.decodeByteArray( data, 0, data.length );
                	myPic.compress( Bitmap.CompressFormat.JPEG, 100, bos );
     
     
                	myPic = BitmapFactory.decodeByteArray( data, 0, data.length );
                	FileOutputStream stream = openFileOutput( "bobblePic3.jpg", MODE_PRIVATE );
                	myPic.compress( Bitmap.CompressFormat.JPEG, 100,stream );
     
                    imageString = new String(bos.toString()); //
                    fOut = openFileOutput("bob.jpg",MODE_PRIVATE);  
                    osw = new OutputStreamWriter(fOut); 
                    osw.write(imageString); 
                    osw.flush();
                    fOut.close();
                    osw.close();
                    bos.close();
     
                } 
                catch (Exception e) {       
                	Log.e("error", e.toString());
                } 
            }
        };
    Je vous remercie par avance pour toutes indications.

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Citation Envoyé par Folkene Voir le message
    je voudrais convertir un byte Array contenant les informations d'une image prise depuis la camera en string.
    Quelle genre de string attend tu? Si tu veux la conversion hexa des bytes en String, utilisevpeut être Arrays.deepToString(byteArray).

    Maintenant ton code n'a pas vraiment de sens! Tu va prendre un byteArrayputputStream, dans lequel tu va appeler toString (qui te retournera un String en "Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding."), donc vatronquer les byte incorrect suivant l'encodage, puis t'essaie de mettre ça dans un fichier et t'espère que ce sera lisible ??

    C'est quoi le but de l'opération?

  3. #3
    Inscrit

    Profil pro
    Inscrit en
    Février 2008
    Messages
    658
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 658
    Points : 892
    Points
    892
    Par défaut
    ByteArray alpha;
    String str= new String ( alpha). Ceci permet de convertir d'un tableau de byte en String.

    J'ai juste repondu au titre du message, les fonctionnalités.

  4. #4
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    26
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 26
    Points : 16
    Points
    16
    Par défaut
    Citation Envoyé par tchize_ Voir le message

    C'est quoi le but de l'opération?
    Je pense que c'est un peu idiot, mais je souhaite transférer mon image en utilisant un web service. La chaine envoyé correspondrait donc à ce qui est contenu dans le fichier image.

  5. #5
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    on ne peut pas faire comme ça. Tu dois définir toi même un moyen de transférer ton contenu binaire sous forme de String. Souvent, mais pas toujours, pour faire ce genre de chose on utilise un encodage de type "base64".

  6. #6
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    26
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 26
    Points : 16
    Points
    16
    Par défaut
    Citation Envoyé par tchize_ Voir le message
    Souvent, mais pas toujours, pour faire ce genre de chose on utilise un encodage de type "base64".
    Existe-t-il un paramètre dans toString permettant l'encodage en base 64, ou une classe d'encodage. J'ai regarder sur l'android developpers et je ne trouve rien sauf webview.

    Au passage merci pour m'avoir aidé

  7. #7
    Inscrit

    Profil pro
    Inscrit en
    Février 2008
    Messages
    658
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 658
    Points : 892
    Points
    892
    Par défaut
    Cette portion de code qu'on a utilisé lorsqu'on voudrais mettre un protocole xmlrpc pour en Android.

    I regroupe des methodes qui te seront utiles, je ne sais pas réellement ce que tu veux mais je poste cette solution esperant que ca pourrai t'aider.

    La syntaxe est tres simple : De n'importe quelle type de format ( byte array, String) l'encoder , decoder, coder en un autre format via Base64.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
     
    package org.sidibe.xmlrpc.android;
     
     
    class Base64Coder {
     
     
     
    	private static char[] map1 = new char[64];
    	static {
    		int i = 0;
    		for (char c = 'A'; c <= 'Z'; c++) {
    			map1[i++] = c;
    		}
    		for (char c = 'a'; c <= 'z'; c++) {
    			map1[i++] = c;
    		}
    		for (char c = '0'; c <= '9'; c++) {
    			map1[i++] = c;
    		}
    		map1[i++] = '+';
    		map1[i++] = '/';
    	}
     
     
    	private static byte[] map2 = new byte[128];
    	static {
    		for (int i = 0; i < map2.length; i++) {
    			map2[i] = -1;
    		}
    		for (int i = 0; i < 64; i++) {
    			map2[map1[i]] = (byte) i;
    		}
    	}
     
    	// Encode   string en Base64 format. 
    	static String encodeString(String s) {
    		return new String(encode(s.getBytes()));
    	}
     
    	// Encode byte array en  Base64 format. 
    		static char[] encode(byte[] in) {
    		return encode(in, in.length);
    	}
     
    	// Encode byte array en  Base64 format. 
     
    	static char[] encode(byte[] in, int iLen) {
    		int oDataLen = (iLen * 4 + 2) / 3; 
    		int oLen = ((iLen + 2) / 3) * 4;
    		char[] out = new char[oLen];
    		int ip = 0;
    		int op = 0;
    		while (ip < iLen) {
    			int i0 = in[ip++] & 0xff;
    			int i1 = ip < iLen ? in[ip++] & 0xff : 0;
    			int i2 = ip < iLen ? in[ip++] & 0xff : 0;
    			int o0 = i0 >>> 2;
    			int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
    			int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
    			int o3 = i2 & 0x3F;
    			out[op++] = map1[o0];
    			out[op++] = map1[o1];
    			out[op] = op < oDataLen ? map1[o2] : '=';
    			op++;
    			out[op] = op < oDataLen ? map1[o3] : '=';
    			op++;
    		}
    		return out;
    	}
     
     
    // Cette methode decode   en  string des données   sous format Base64.
     
    	 	static String decodeString(String s) {
    		return new String(decode(s));
    	}
     
    	static byte[] decode(String s) {
    		return decode(s.toCharArray());
    	}
     
     
     // Cette methode decode un byteArray  des donnée de format Base64
     
    	static byte[] decode(char[] in) {
    		int iLen = in.length;
    		if (iLen % 4 != 0) {
    			throw new IllegalArgumentException(
    					"Length of Base64 encoded input string is not a multiple of 4.");
    		}
    		while (iLen > 0 && in[iLen - 1] == '=') {
    			iLen--;
    		}
    		int oLen = (iLen * 3) / 4;
    		byte[] out = new byte[oLen];
    		int ip = 0;
    		int op = 0;
    		while (ip < iLen) {
    			int i0 = in[ip++];
    			int i1 = in[ip++];
    			int i2 = ip < iLen ? in[ip++] : 'A';
    			int i3 = ip < iLen ? in[ip++] : 'A';
    			if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127) {
    				throw new IllegalArgumentException(
    						"Illegal character in Base64 encoded data.");
    			}
    			int b0 = map2[i0];
    			int b1 = map2[i1];
    			int b2 = map2[i2];
    			int b3 = map2[i3];
    			if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0) {
    				throw new IllegalArgumentException(
    						"Illegal character in Base64 encoded data.");
    			}
    			int o0 = (b0 << 2) | (b1 >>> 4);
    			int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2);
    			int o2 = ((b2 & 3) << 6) | b3;
    			out[op++] = (byte) o0;
    			if (op < oLen) {
    				out[op++] = (byte) o1;
    			}
    			if (op < oLen) {
    				out[op++] = (byte) o2;
    			}
    		}
    		return out;
    	}
     
     
    	private Base64Coder() {
    	}
    }

  8. #8
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    26
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 26
    Points : 16
    Points
    16
    Par défaut
    Merci pour vos réponse, je vais tester et poster ma solution

  9. #9
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    26
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 26
    Points : 16
    Points
    16
    Par défaut
    Bonjour,
    Mon problème n'est pas résolu. J'ai utilisé la class de jahbromo pour encoder mon byteArray en base64. Le problème arrive lorsque je l'envoie à mon WebService (Axis), celui me renvoie une SoapException indiquant que le format de l'image est erroné. D'après le WSDL, le format attendu est base64Binary. Si quelqu'un à une idée ou une solution merci de partager.

    PS: J'utilise KSOAP2 pour appeler mon WebService

  10. #10
    Inscrit

    Profil pro
    Inscrit en
    Février 2008
    Messages
    658
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 658
    Points : 892
    Points
    892
    Par défaut
    Renvoie nous le code;

Discussions similaires

  1. Réponses: 4
    Dernier message: 31/12/2009, 16h24
  2. Flex / AIR String to ByteArray
    Par Cedwik dans le forum Flex
    Réponses: 1
    Dernier message: 14/04/2008, 11h25
  3. Flex / AIR String to ByteArray
    Par Cedwik dans le forum Flash
    Réponses: 8
    Dernier message: 12/04/2008, 18h24
  4. Concaténation String+ByteArray ?
    Par gdev7 dans le forum Langage
    Réponses: 1
    Dernier message: 18/02/2008, 12h54
  5. String Grid et choix d'une couleur pour une ligne
    Par Gigottine dans le forum C++Builder
    Réponses: 12
    Dernier message: 17/05/2002, 15h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo