Java convertit les images en binaire, puis convertit les binaires en images

  1. importer  java.awt.image.BufferedImage ;      
  2. importer  java.io.ByteArrayInputStream ;      
  3. importer  java.io.ByteArrayOutputStream ;      
  4. importer  java.io.File ;      
  5. importer  java.io.IOException ;      
  6.      
  7. importer  javax.imageio.ImageIO ;      
  8.      
  9. importer  sun.misc.BASE64Decoder ;      
  10. importer  sun.misc.BASE64Encoder ;      
  11.      
  12.  classe publique  TestImageBinary {      
  13.     encodeur statique  BASE64Encoder =  new  sun.misc.BASE64Encoder();      
  14.     décodeur statique  BASE64Decoder =  new  sun.misc.BASE64Decoder();      
  15.           
  16.     public  static  void  main (String[] arguments) {      
  17.         System.out.println(getImageBinary());      
  18.               
  19.         base64StringToImage(getImageBinary());      
  20.     }      
  21.           
  22.     Chaîne statique  getImageBinary(){      
  23.         Fichier f =  nouveau  fichier( "c://20090709442.jpg" );             
  24.         BufferedImage bi;      
  25.         essayez  {      
  26.             bi = ImageIO.read(f);      
  27.             ByteArrayOutputStream baos =  new  ByteArrayOutputStream();      
  28.             ImageIO.write(bi,  "jpg" , baos);      
  29.             octet [] octets = baos.toByteArray();      
  30.                   
  31.             return  encoder.encodeBuffer(bytes).trim();      
  32.         }  catch  (IOException e) {      
  33.             e.printStackTrace();      
  34.         }      
  35.         renvoie  null ;      
  36.     }      
  37.           
  38.     statique  vide  base64StringToImage(String base64String){      
  39.         essayez  {      
  40.             octet [] octets1 = decoder.decodeBuffer(base64String);      
  41.                   
  42.             ByteArrayInputStream bais =  new  ByteArrayInputStream(bytes1);      
  43.             BufferedImage bi1 =ImageIO.read(bais);      
  44.             Fichier w2 =  new  File( "c://QQ.bmp" );//peut être au format jpg, png ou gif      
  45.             ImageIO.write(bi1,  "jpg" , w2); //Quel que soit le format de sortie de l'image, aucune modification n'est nécessaire ici.      
  46.         }  catch  (IOException e) {      
  47.             e.printStackTrace();      
  48.         }      
  49.     }      
  50.      
  51. }   

おすすめ

転載: blog.csdn.net/linwei_hello/article/details/44624583