[Java] java十六进制转成图片 →→→→→进入此内容的聊天室

来自 , 2019-12-15, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/708f3cf8
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.nio.ByteBuffer;
  10. import java.nio.channels.FileChannel;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13.  
  14. /**
  15.  * 十六进制转成图片
  16.  * @author Administrator
  17.  *
  18.  */
  19. public class Hex2Image {
  20.     public static void main(String[] args) throws Exception {
  21.         Hex2Image to=new Hex2Image();
  22.         InputStream is=new FileInputStream("f://aa.txt");
  23.         InputStreamReader isr = new InputStreamReader(is);
  24.         BufferedReader br = new BufferedReader(isr);
  25.         String str = null;
  26.         StringBuilder sb = new StringBuilder();
  27.         while ((str = br.readLine()) != null) {
  28.             System.out.println(str);
  29.             sb.append(str);
  30.         }
  31.         to.saveToImgFile(sb.toString().toUpperCase(),"f://dd.jpg");
  32.  
  33.     }
  34.     public void saveToImgFile(String src,String output){
  35.            if(src==null||src.length()==0){
  36.                return;
  37.            }
  38.            try{
  39.                FileOutputStream out = new FileOutputStream(new File(output));
  40.                byte[] bytes = src.getBytes();
  41.                for(int i=0;i<bytes.length;i+=2){
  42.                    out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));
  43.                }
  44.                out.close();
  45.            }catch(Exception e){
  46.                e.printStackTrace();
  47.            }
  48.        }
  49.        private int charToInt(byte ch){
  50.            int val = 0;
  51.            if(ch>=0x30&&ch<=0x39){
  52.                val=ch-0x30;
  53.            }else if(ch>=0x41&&ch<=0x46){
  54.                val=ch-0x41+10;
  55.            }
  56.            return val;
  57.        }
  58. }
  59.  
  60. //源代码片段来自云代码http://yuncode.net
  61.                        

回复 "java十六进制转成图片"

这儿你可以回复上面这条便签

captcha