iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java中将base64编码字符串转换为图片的代码
  • 364
分享到

Java中将base64编码字符串转换为图片的代码

Python 官方文档:入门教程 => 点击学习

摘要

前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图片 1、将base64编码字符串转换为图片

前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图片

1、将base64编码字符串转换为图片的代码如下 ImageUtil.java:

package util;

import javax.servlet.Http.httpservletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.UUID;

public class ImageUtil {

	
	public static String generateImage(String file, String path, HttpServletRequest request) {
		// 解密
		try {
			// 项目绝对路径
			String savePath = request.getSession().getServletContext().getRealPath("upload");
			// 图片分类路径+图片名+图片后缀
			String imGClassPath = path.concat(UUID.randomUUID().toString()).concat(".jpg");
			// 解密
			Base64.Decoder decoder = Base64.getDecoder();
			// 去掉base64前缀 data:image/jpeg;base64,
			file = file.substring(file.indexOf(",", 1) + 1, file.length());
			byte[] b = decoder.decode(file);
			// 处理数据
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {
					b[i] += 256;
				}
			}
			// 保存图片
			OutputStream out = new FileOutputStream(savePath.concat(imgClassPath));
			out.write(b);
			out.flush();
			out.close();
			// 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
			return imgClassPath;
		} catch (IOException e) {
			return null;
		}
	}
}

补充:Java实现图片转base64字符串和图片互相转换

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import java.io.*;
 

public class Test010 {
 
    public static void main(String[] args) {
        String base64Str = imageToBase64Str("D:\\SoftWare\\图片素材\\头像\\432.jpeg");
        System.out.println(base64Str);
 
        boolean b = base64StrToImage(base64Str, "D:\\002.jpg");
        System.out.println(b);
    }
 
    
    public static String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
 
    
    public static boolean base64StrToImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            //文件夹不存在则自动创建
            File tempFile = new File(path);
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(tempFile);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
}

到此这篇关于Java中将base64编码字符串转换为图片的文章就介绍到这了,更多相关Java base64编码字符串转换为图片内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java中将base64编码字符串转换为图片的代码

本文链接: https://www.lsjlt.com/news/200346.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作