- importer java.awt.image.BufferedImage ;
- importer java.io.ByteArrayInputStream ;
- importer java.io.ByteArrayOutputStream ;
- importer java.io.File ;
- importer java.io.IOException ;
- importer javax.imageio.ImageIO ;
- importer sun.misc.BASE64Decoder ;
- importer sun.misc.BASE64Encoder ;
- classe publique TestImageBinary {
- encodeur statique BASE64Encoder = new sun.misc.BASE64Encoder();
- décodeur statique BASE64Decoder = new sun.misc.BASE64Decoder();
- public static void main (String[] arguments) {
- System.out.println(getImageBinary());
- base64StringToImage(getImageBinary());
- }
- Chaîne statique getImageBinary(){
- Fichier f = nouveau fichier( "c://20090709442.jpg" );
- BufferedImage bi;
- essayez {
- bi = ImageIO.read(f);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ImageIO.write(bi, "jpg" , baos);
- octet [] octets = baos.toByteArray();
- return encoder.encodeBuffer(bytes).trim();
- } catch (IOException e) {
- e.printStackTrace();
- }
- renvoie null ;
- }
- statique vide base64StringToImage(String base64String){
- essayez {
- octet [] octets1 = decoder.decodeBuffer(base64String);
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
- BufferedImage bi1 =ImageIO.read(bais);
- Fichier w2 = new File( "c://QQ.bmp" );//peut être au format jpg, png ou gif
- ImageIO.write(bi1, "jpg" , w2); //Quel que soit le format de sortie de l'image, aucune modification n'est nécessaire ici.
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Java convertit les images en binaire, puis convertit les binaires en images
おすすめ
転載: blog.csdn.net/linwei_hello/article/details/44624583
おすすめ
ランキング