16进制字符串与JPG图片互转

字符串与图片互转

昨天在完成一个16进制转字符串的任务,觉得有必要记录一下。

import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
public class Hex2Image {
        public static void main(String[] args) throws Exception {
            String str=null;
            try
            {
                StringBuffer sb = new StringBuffer();
                FileInputStream fis = new FileInputStream("D:\\111.jpg");//图片路径
                //BufferedInputStream bis = new BufferedInputStream(fis);
                java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
                byte[] buff = new byte[1024];
                int len = 0;
                while ((len = fis.read(buff)) != -1)
                {
                    bos.write(buff, 0, len);
                }
                // 得到图片的字节数组
                byte[] result = bos.toByteArray();
                // 字节数组转成十六进制
                str = toHexString(result);
                PrintWriter pw = new PrintWriter(
                        new FileWriter("aaa.txt"));
                pw.println(str);
                pw.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            System.out.println(str);
            byte[] t=fromHexString(str);
            byte2image(t, "D:\\sfz10.jpg");//存放图片路径
        }
    static public String toHexString(byte[] ba) {
        StringBuilder sbuf = new StringBuilder();
        for (byte b : ba) {
            String s = Integer.toHexString((int) (b & 0xff));
            if (s.length() == 1) {
                sbuf.append('0');
            }
            sbuf.append(s);
        }
        return sbuf.toString();
    }
    public static byte[] fromHexString(String s) {
        int length = s.length() / 2;
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            bytes[i] = (byte) ((Character.digit(s.charAt(i * 2), 16) << 4) | Character
                    .digit(s.charAt((i * 2) + 1), 16));
        }
        return bytes;
    }
    public static void byte2image(byte[] data,String path){
        if(data.length<3||path.equals("")) return;
        try{
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        } catch(Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }
    }

在处理接口请求到的16进制图片时,注意看一下有无标识符,我就在这里卡了一上午,眼拙没看出来…
当时我请求到的字符串是有"\x"标识符。
这里我的处理方法是:replace("\\").replace("x")
参考文章:https://www.cnblogs.com/qgc88/p/5555154.html

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值