Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2005
    Messaggi
    6

    Trasformare immagini in testo e viceversa

    Ciao a tutti, avrei bisogno di aiuto; sto cercando di convertire un immagine in testo, come ad esempio si fa con le email, quando si vede il sorgente dei file allegati, credo che sia in base64 ma non riesco a trovare informazioni su come poter fare.
    Quello che devo fare è: aprire un immagine e poi scrivere su di un file di testo (come le email) l'immagine trasformata in testo, e poi poter rileggerla e ritrasformarla in immagine.

    Grazie
    e scusate se ho postato due volte......
    Ciao Pao.


  2. #2
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Prova a cercare informazioni sulla codifica Base64.
    Se ti può essere utile, questa classe fa proprio quello che chiedi: prende in input un file (qualsiasi!!) e lo codifica Base64.
    codice:
    import java.io.*;
    
    public class MIMEBase64 {
    
     static String BaseTable[] = {
        "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
        "Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
        "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
        "w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"
        };
    
      public static void encode(String filename, BufferedWriter out) {
        try {
          File f              = new File(filename);
          FileInputStream fin = new FileInputStream(filename);
    
          // read the entire file into the byte array
          byte bytes[]        = new byte[(int)(f.length())];
          int n               = fin.read(bytes);
    
          if (n < 1) return;          // no bytes to encode!?!
    
          byte buf[] = new byte[4];   // array of base64 characters
    
          int n3byt      = n / 3;     // how 3 bytes groups?
          int nrest      = n % 3;     // the remaining bytes from the grouping
          int k          = n3byt * 3; // we are doing 3 bytes at a time
          int linelength = 0;         // current linelength
          int i          = 0;         // index
    
          // do the 3-bytes groups ...
          while ( i < k ) {
             buf[0] = (byte)(( bytes[i]   & 0xFC) >> 2);
             buf[1] = (byte)(((bytes[i]   & 0x03) << 4) |
                      ((bytes[i+1] & 0xF0) >> 4));
             buf[2] = (byte)(((bytes[i+1] & 0x0F) << 2) |
                      ((bytes[i+2] & 0xC0) >> 6));
             buf[3] = (byte)(  bytes[i+2] & 0x3F);
             send(out, BaseTable[buf[0]]);
             send(out, BaseTable[buf[1]]);
             send(out, BaseTable[buf[2]]);
             send(out, BaseTable[buf[3]]);
    
           if ((linelength += 4) >= 76) {
              send(out, "\r\n");
              linelength = 0;
              }
           i += 3;
           }
    
         // deals with with the padding ...
         if (nrest==2) {
            // 2 bytes left
            buf[0] = (byte)(( bytes[k] & 0xFC)   >> 2);
            buf[1] = (byte)(((bytes[k] & 0x03)   << 4) |
                       ((bytes[k+1] & 0xF0) >> 4));
            buf[2] = (byte)(( bytes[k+1] & 0x0F) << 2);
            }
         else if (nrest==1) {
            // 1 byte left
            buf[0] = (byte)((bytes[k] & 0xFC) >> 2);
            buf[1] = (byte)((bytes[k] & 0x03) << 4);
            }
         if (nrest > 0) {
            // send the padding
            if ((linelength += 4) >= 76) send(out, "\r\n");
            send(out, BaseTable[buf[0]]);
            send(out, BaseTable[buf[1]]);
            // Thanks to R. Claerman for the bug fix here!
            if (nrest==2) {
               send(out, BaseTable[buf[2]]);
               }
            else {
              send(out, "=");
              }
            send(out, "=");
            }
          out.flush();
          fin.close();
          }
         catch (Exception e) {
           e.printStackTrace();
           }
       }
    
     public static void send(BufferedWriter out, String s) {
        try {
          out.write(s);
          }
        catch (Exception e) {
          e.printStackTrace();
          }
        }
     }
    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2005
    Messaggi
    6
    grazie provo subito...

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2005
    Messaggi
    6
    grazie!!!
    Provato e funziona...
    Ma ora devo ricavarmi il decode, visto che li c'era solo encode...
    per fare questo vista la mia ignoranza, mi potreste spiegare il significato di queste istruzioni?
    buf[1] = (byte)(((bytes[i] & 0x03) << 4) | ((bytes[i+1] & 0xF0) >> 4));

    Così magari riesco a farmi il decode.....grazie ancora...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.