java将base64字符串的图片转换成本地图片、本地图片转base64字符串

Java:Base64转为图片 / 去掉base64前缀 data:image/jpeg;* 文件路径:到文件夹即可,代码里会在文件夹里生成对应的jpg文件。log.info("jpg文件创建成功");log.info("jpg文件创建失败");log.info("文件未找到");log.info("写入失败");// 写入成功返回文件路径。return "文件不存在";// 判断文件路径是否存在。* base64转为图片。 阅读详情
/**
     * 将base64的字符串转成图片
     * @param imgStr base64图片的字符串
     * @param imgFilePath 本地图片存储路径
     * @return
     */
    public static boolean generateImage(String imgStr,String imgFilePath) {
        if(imgStr == null) {
            return false;
        }
        byte[] b;
        try {
            b = new BASE64Decoder().decodeBuffer(imgStr);
            for (int i = 0; i < b.length; i++) {
                if(b[i]<0) {
                    b[i]+=256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }





/**
 * 将本地文件转成base64字符串
 * @return
 */
public static String file2Byte(String filePath) {
    byte[] data = null;
    FileImageInputStream input = null;
    try {
        input = new FileImageInputStream(new File(filePath));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int length = 0;
        while((length=input.read(buf))!=-1) {
            out.write(buf,0,length);
        }
        data = out.toByteArray();
        out.close();
        input.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new BASE64Encoder().encode(data);
}

▄█▀█●老铁,站住!别跑,我又不收你钱,如果我的demo能够帮助到你,请给我一个赞吧!

[java] 图片与base64之间的互相转换 这篇文章实现的功能是,本地或者线上的图片转换成base64和base64转换成图片。好了不多说了,直接上代码!import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io... 阅读详情

相关推荐

告别Sourceforge安装器!手把手教你下载MinGW-w64离线包并配置环境变量(Windows 10/11)

本文详细介绍了如何在Windows 10/11系统上通过下载MinGW-w64离线包并配置环境变量,完全绕过Sourceforge安装器的问题。从选择正确的版本、解压到本地目录,到设置系统环境变量和验证编译环境,手把手指导开发者快速搭建稳定的GCC编译环境,解决常见的安装困扰。

weixin_30896825的博客 338

JAVA 把base64图片数据转为本地图片

项目中用到的把base64图片数据转为本地图片的函数 /** * 替换html中的base64图片数据为实际图片 * @param html * @param fileRoot 本地路径 * @param serRoot 服务器路径 * @return */ public static String replaceBase64Image(String html,Stri

ForeverCjl的专栏 1万+

最小路径和——动态规划求解(Java实现)

最小路径和——动态规划求解(Java实现) 题目: 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。 示例: 输入: [ [1,3,1], [1,5,1], [4,2,1] ] 输出: 7 解释: 因为路径 1→3→1→1→1 的总和最小。 这题和机器人走路的思路类似,就是状态方程有所不同,...

weixin_43395911的博客 896

遇到BASE64的图片字符串应该如何转换成图片呢

遇到BASE64的图片字符串应该如何转换成图片呢 base64.b64decode(data)

野路子云遇见不同的自己 3167

HMMER实战:从零部署到序列分析

本文详细介绍了HMMER工具的安装与使用,从源码编译到环境配置,再到核心工具hmmbuild、hmmsearch等的实战应用。通过球蛋白序列分析案例,展示了HMMER在生物信息学中的强大功能,帮助读者快速掌握序列分析技巧。

weixin_42529366的博客 258

【Unity3D】RTS Engine 笔记一

RTS Engine 中配置一个新场景, 初步认识GameManager、设置地形以及配置地形网格搜索

ViYeye的博客 1653

Java如何将 Base64 编码转化为可读图片

Base64 是网络上较常见的用于传输 8Bit 字节码的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法。

佛系豪豪 2791

别再让信号‘撞墙’了!手把手教你用嘉立创阻抗计算器搞定PCB高速线(附免费压层结构选择)

本文详细介绍了如何使用嘉立创阻抗计算器解决PCB高速线设计中的阻抗匹配问题,避免信号反射和传输损耗。通过免费压层结构选择和五步计算法,帮助工程师快速掌握USB3.0差分线等高速信号的设计技巧,提升PCB设计效率和信号质量。

weixin_30736301的博客 282

【Uniapp】base64图片资源转为本地图片,解决canvas不支持base64问题

因为canvas不支持base64,其次在使用小程序 canvas 的 drawImage api,需要将网络图片转为本地图>片,base64不能通过uni.downloadFile这种方式下载文件资源到本地,所以在真机上,无法正常显示海>报。通过接口获取到base64类型的二维码,把二维码放到canvas里生成海报。在微信小程序开发工具中能够正常显示海报,到真机上测试就无法显示二维码。

ColdCoder的博客 2505

「Debug R」在从名字空间‘XVector’中出口方法时发现了不是S4通用的函数‘parallelSlotNames’...

每当R语言版本更新,Bioconductor的版本随之更替的时候,总会有一些神奇的报错会出现,比如说下面这种。 Error: package or namespace load failed for ‘GenomicRanges’: 在从名字空间‘XVector’中出口方法时发现了不是S4通用的函数‘parallelSlotNames’ 如果读中文读不懂,我们可以把它翻译成英文。 packa...

xuzhougeng blog 2003

Spring AI 项目实战(十八):Spring Boot + AI + Vue3 + OSS + DashScope 实现高效语音识别系统(附完整源码)

本文介绍基于 Spring Boot AI 集成阿里云 OSS 与 DashScope 语音识别 API 的高效语音识别系统。该系统通过云端存储音频文件,解决传统本地存储的性能瓶颈与存储压力,支持同步 / 异步语音转文本,实现文件自动生命周期管理。文中详述技术选型、架构设计、核心代码实现(含前后端及配置)、部署流程,展现其高并发处理、可靠文件管理能力,适用于客服、教育等企业级场景,为语音识别应用提供实用解决方案。

博客 1537

Java读取本地图片变成字符串_java将base64字符串的图片转换成本地图片、本地图片转base64字符串...

/*** 将base64的字符串转成图片* @param imgStr base64图片的字符串* @param imgFilePath 本地图片存储路径* @return*/publicstatic boolean generateImage(String imgStr,String imgFilePath) {if(imgStr == null) {return false;}byte[] ...

weixin_34847198的博客 175

Livox激光雷达传感器在Python3中的完整驱动程序:深入剖析OpenPyLivox库的实现和应用

当我们提到激光雷达或激光扫描技术时,Livox激光雷达传感器无疑是业界的佼佼者。尽管Livox提供了基于C++的官方软件和API,但现在,有了OpenPyLivox (OPL) 库,Python开发者们也能够轻松地接入和控制这些高级的激光雷达设备。

qq_38334677的博客 1002

java后台接受base64格式并转换为图片解决文件损坏问题

直接后台代码,因为接受的base64字符前面data:image/jpeg;base64,不是照片内容,去掉即可,否则会文件不识别(损坏) /** * 新增保存附件 */ @PostMapping("/add") @ResponseBody public ResultBody addSave(HttpServletRequest request) throws IOException { Resu...

weixin_44674299的博客 7018

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

本地图片/网络图片转换成Base64字符串

下面先介绍第一种:把网络图片转换成Base64字符串 第二种:把本地图片转换成base64 由此两种方法都介绍完了,下面附上源码: package zhang; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net....

一介书生的博客 2414

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

图片转Base64编码 base64编码转图片

这两天给手机写了几个服务(接口形式),其他数据还好,图片实在没处理过,这里记录下使用base64编码遇到的坑。。。。 1、图片转base64编码: public static String getImageStr(String imgUrl) {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理 String imgFile = imgUrl;// 待处理的

fanxiangru999的专栏 5007

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

一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图片 1、将base64编码字符串转换为图片的代码如下 ImageUtil.java: package util; import javax.servlet.http.HttpServletRequest; import java.io.FileOutputSt...

JACKAL 23万+

Base64转图片与图片转Base64

在工作中可能会遇到base64与图片的相互转化。 图片转base64 String file="D://picpath//video//test.png" InputStream in = null; byte[] data = null; // 读取图片字节数组 try { in = new FileInputStream(file); data = new byte[in.available()];

lovely960823的博客 6705

java将图片转为base64字符串

base64字符串与图片相互转换的核心就是文件流与字节数组的相互转换,由于转换简单下面直接贴代码: package com.rh.fs.util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;...

huangshikai 2765

本地图片转换Base64的方法,Base64码转换为本地图片

使用须知: 1.本地图片转换为Base64码 2.Base64码转换为本地图片 一:本地图片转换Base64的方法 1:java代码 /** * 本地图片转换Base64的方法 * @param imgPath */ public static String ImageToBase64(String imgPath) { ...

Mr.Chen的博客 9099

java二进制转图片base64_java通过base64对图片进行转换来缓存图片

缓存图片总不能将图片的url缓存进来吧,那还不如直接请求图片服务器(虽然这里已经转换成功的,但是如果图片大的话,编码也很长,性能也会变差,大神可以告诉我下怎么压缩一下这个base64吗)可以使用base64 进行对图片的转换,然后将转换后的图片进行保存进缓存,然后通过将base64 转换后的图片编码返回前端就ok了这是base64 的一个工具类import java.awt.image.Buffe...

weixin_29193259的博客 1662

图片转Base64字符串并解析

Base64字符串是一种用于将二进制数据编码为文本字符串的编码方式。这种编码方式的目的是在传输或存储时保证数据的安全性和可读性。Base64编码的原理是将二进制数据转换成一组由64个不同ASCII字符组成的文本字符串。

qq_45110186的博客 4662

js file转base64_图片优雅的转换成Base64

我们知道图片有很多种存储和表达形式,那么在开发中遇到图片转Base64的需求也是非常常见的,今天我们就来看看如何将图片转换成Base64字符串的。注意:此处会用到sun.misc.BASE64Encoder工具类,这个包并不自带,建议升级到JDK8,若要下载该包请在公众号中回复20200606即可获得下载地址NO.1 【基本工具】publicstaticString...

weixin_39617473的博客 1579

java 图片转Base64及Base64转图片

参考: Java 工具箱 | 图片-Base64 互转 一、base 64简介 Data URLs 由如下四个部分组成,如文章开篇的 data:image/jpeg;base64,/9j/4AAQSkxxxxxxx data:[<mediatype>][;base64],<data> data: : 协议固定前缀。 [<mediatype>] : 是一个 MIME type,比如 image/jpeg,你也可以在 "Incomplete list of MIME t

火星男孩的分享空间 2万+

java将图片转base64格式

代码示例 package demo; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.Objects; public class Base64Demo { public static void main(String[

彭世瑜的博客 4068

java图片转base64转图片

在线图片转换Base64 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; public class ImageToBase64ToImage { public static void main(String[] args) { String strImg = I...

吾行远方 4486
上一篇: Springboot controller使用阿里巴巴的fastJson包
下一篇: java通过SmbFile操作远程共享盘内文件(非网络挂在盘,类似\\192.168.1.1这种共享盘)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值