2010-05-06 4 views

Répondre

1

Le Apache Commons Base64 pour l'encodage et le décodage

+3

Habituellement, il vaut la peine d'ajouter un exemple simple ou un petit extrait du lien, pour fournir plus de contexte et augmenter la valeur de la réponse. –

2

code Java pour convertir l'image en chaîne

package com.test; 

import java.io.IOException; 

import sun.misc.BASE64Encoder; 
import sun.misc.BASE64Decoder; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.awt.image.BufferedImage; 
import javax.imageio.ImageIO; 

public class Test{ 
public static void main (String args[]) throws IOException { 
BufferedImage img = ImageIO.read(new File("C:/Test/logo.png")); 
     BufferedImage newImg; 
     String imgstr; 
imgstr = encodeToString(img, "png"); 
     System.out.println(imgstr); 
} 
public static String encodeToString(BufferedImage image, String type) { 
     String imageString = null; 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     try { 
      ImageIO.write(image, type, bos); 
      byte[] imageBytes = bos.toByteArray(); 

      BASE64Encoder encoder = new BASE64Encoder(); 
      imageString = encoder.encode(imageBytes); 

      bos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return imageString; 
    } 
} 

et peut l'intégrer dans XSL comme ci-dessous

<img src="data:image/png;base64,iVBORw0......."/> 
Questions connexes