java读取图片为字符串,再将字符串转为图片

import com.alibaba.fastjson.JSON;
import com.pfx.common.domain.PFXResultInfo;
import com.pfx.pfxTool.PfxTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.HttpClient;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ImageToString {
    private static Log log = LogFactory.getLog(ImageToString.class);

    // 根据图片地址将图片转换为字符串类型的数据
    public static String imageToString(String picture) {
        StringBuffer sb2 = new StringBuffer();
        BufferedImage image1 = getImage(picture);
        byte[] img = getBytes(image1);

        for (int i = 0; i < img.length; i++) {
            if (sb2.length() == 0) {
                sb2.append(img[i]);
            } else {
                sb2.append("," + img[i]);
            }
        }
        return sb2.toString();

    }

    // 将BufferImage 转换为字节数组
    private static byte[] getBytes(BufferedImage image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "PNG", baos);
        } catch (Exception e) {
            log.info(e);
        }
        return baos.toByteArray();
    }

    // 根据图片地址得到BufferedImage
    public static BufferedImage getImage(String picture) {

        try {

            BufferedImage bImage = ImageIO.read(new File(picture));
            return bImage;
        } catch (Exception ex) {
            log.info(ex);
            return null;
        }

    }

    //==========================
    public static String fileformat = "png";
    public static String fileNameFormat = "yyyy-MM-dd_HH-mm-ss";

    // 将字符串格式的图片转换为图片并保存
    public static void stringToImage(String string, String saveDir) {
        if (string.contains(",")) {
            // 这里没有自带的那个分割方法,原因是分割速度没有这个快,有人考证在分割字符长度很大的情况下,系统的分割方法容易造成内存溢出。
            // 还有subString方法,不知道最新版本的jdk改了源码了么
            String[] imagetemp = split(string, ",");
            byte[] image = new byte[imagetemp.length];
            for (int i = 0; i < imagetemp.length; i++) {
                image[i] = Byte.parseByte(imagetemp[i]);
            }
            saveImage(image, saveDir);
        } else {
            // 不能解析格式的字符串
        }
    }

    // 将byte[] 转换为BufferedImage
    private static BufferedImage readImage(byte[] bytes) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        return ImageIO.read(bais);
    }

    // 保存图片
    public static String saveImage(byte[] imgages, final String saveDir) {
        try {
            BufferedImage bis = readImage(imgages);
            DateFormat sdf = new SimpleDateFormat(fileNameFormat);
            String fileTime = sdf.format(new Date());
            final String name = fileTime + "_" + "." + fileformat;
            File f = new File(saveDir + name);
            boolean istrue = false;
            if (f.exists()) {
                istrue = ImageIO.write(bis, fileformat, f);
            } else {
                f.mkdirs();
                istrue = ImageIO.write(bis, fileformat, f);
            }
            if (istrue) {
                return name;
            }
        } catch (Exception e) {
        }
        return null;
    }

    // 分割字符串
    public static String[] split(String s, String token) {
        if (s == null)
            return null;
        if (token == null || s.length() == 0)
            return new String[]{s};
        int size = 0;
        String[] result = new String[4];
        while (s.length() > 0) {
            int index = s.indexOf(token);
            String splitOne = s;
            if (index > -1) {
                splitOne = s.substring(0, index);
                s = s.substring(index + token.length());
            } else {
                s = "";
            }
            if (size >= result.length) {
                String[] tmp = new String[result.length * 2];
                System.arraycopy(result, 0, tmp, 0, result.length);
                result = tmp;
            }
            if (splitOne.length() > 0) {
                result[size++] = splitOne;
            }
        }
        String[] tmp = result;
        result = new String[size];
        System.arraycopy(tmp, 0, result, 0, size);
        return result;
    }

/*    public static void main(String[] args) {
        String filePath = "C:\\Users\\84939\\Desktop\\宋佳乐\\abc.png";
        String filePath2 = "C:\\Users\\84939\\Desktop\\坑爹\\是啥.png";

        String s = imageToString(filePath);

        String url = "C:\\Users\\84939\\Desktop\\李科.pfx";
        String password = "1234";
        PfxTool pfxTool = new PfxTool();

        //加密
        PFXResultInfo resultInfo1 = pfxTool.pfxEncrypt(url, password, s);
        System.out.println(resultInfo1.getMessage());
        //解密
        PFXResultInfo resultInfo2 = pfxTool.pfxDecrypt(url, password, resultInfo1.getMessage());
        System.out.println(resultInfo2.getMessage());

        stringToImage(resultInfo2.getMessage(), filePath2);
    }*/

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喝茶儿摸鱼儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值