导航:首页 > 编程语言 > javabase64toimage

javabase64toimage

发布时间:2022-08-20 14:46:50

① 图片经由过程base64编码后,怎么将其复原在image中显示

这个需要jsp的java或者php这样的有操控能力的环境才行,而浏览器端的js实现虽可能但比较麻烦
java而言,先Base64Decoder.decode(原数据);然后以ImageIO读入解码后的数据,生成Image
就可以写文件或显示了。
先说这些,给出数据,才好有代码的。

② java转化BASE64为PNG的异常情况

那是部分软件的问题,不是所有的软件都支持这样的base64的。
~
~
~
~~~~~~~~~~~~~~~~~~~

③ java 怎么将base64转成照片

Stringfile="......base64..........";
StringfileName=test+".jpg";
StringfilePath="/home/"+fileName;
byte[]json=null;
try{
json=file.getBytes("UTF-8");
json=Base64.decodeBase64(json);
Filefiles=newFile(filePath);
=null;
try{
imageOutput=newFileImageOutputStream(files);
imageOutput.write(json,0,json.length);
}catch(FileNotFoundExceptione){
_log.info(e.getMessage());
}catch(IOExceptione){
_log.info(e.getMessage());
}
try{
imageOutput.close();
}catch(IOExceptione){
_log.info(e.getMessage());
}
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}

请采纳,谢谢

④ 将base64位转换成png图片的java代码

//base64字符串转化成图片
public static boolean GenerateImage(String imgStr)
{ //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = "d://222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
希望可以帮到你。

⑤ java将base64转换成图片并保存在指定路径下ImageIO.write(bi1,"png",w2)提示image==null

这个简单啊
(1)把获取url流转为bitmap
(2)把bitmap再转为base64
(Stringurlpath){
Bitmapmap=null;
try{
URLurl=newURL(urlpath);
URLConnectionconn=url.openConnection();
conn.connect();
InputStreamin;
in=conn.getInputStream();
map=BitmapFactory.decodeStream(in);
//TODOAuto-generatedcatchblock
}catch(IOExceptione){
e.printStackTrace();
}
returnmap;
}

第二步
/**
*bitmap转为base64
*@parambitmap
*@return
*/
(Bitmapbitmap){

Stringresult=null;
ByteArrayOutputStreambaos=null;
try{
if(bitmap!=null){
baos=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);

baos.flush();
baos.close();

byte[]bitmapBytes=baos.toByteArray();
result=Base64.encodeToString(bitmapBytes,Base64.DEFAULT);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(baos!=null){
baos.flush();
baos.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresult;
}

⑥ Java里谁能给我一个将图片转换成二进制在将二进制转换成图片的demo!!!

importjava.awt.image.BufferedImage;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.IOException;

importjavax.imageio.ImageIO;

importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;

publicclassTestImageBinary{
staticBASE64Encoderencoder=newsun.misc.BASE64Encoder();
staticBASE64Decoderdecoder=newsun.misc.BASE64Decoder();

publicstaticvoidmain(String[]args){
System.out.println(getImageBinary());
base64StringToImage(getImageBinary());
}

staticStringgetImageBinary(){
Filef=newFile("c://a.jpg");
BufferedImagebi;
try{
bi=ImageIO.read(f);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
ImageIO.write(bi,"jpg",baos);
byte[]bytes=baos.toByteArray();

returnencoder.encodeBuffer(bytes).trim();
}catch(IOExceptione){
e.printStackTrace();
}
returnnull;
}

staticvoidbase64StringToImage(Stringbase64String){
try{
byte[]bytes1=decoder.decodeBuffer(base64String);

ByteArrayInputStreams=newByteArrayInputStream(bytes1);
BufferedImagebi1=ImageIO.read(s);
Filew2=newFile("c://index//a.bmp");//可以是jpg,png,gif格式
ImageIO.write(bi1,"jpg",w2);//不管输出什么格式图片,此处不需改动
}catch(IOExceptione){
e.printStackTrace();
}
}

}

⑦ 如何将base64位的字节数组转换成图片并显示

不用保存成文件。

写一个servlet(假设名字是servletImg),页面的参数就是 id,然后将从DB得到的图像的byte[],通过流输出给页面。

页面还是 <img src="servletImg?id=1234" />

这样的逻辑,才行。

⑧ JAVA怎么将pdf的base64转换成jpg的base64

package com.aiait.base.util;


import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.rendering.ImageType;

import org.apache.pdfbox.rendering.PDFRenderer;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import com.aiait.base.service.impl.base.SearchServiceImpl;


import org.apache.pdfbox.*;


import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.text.DecimalFormat;

import java.util.Date;


import javax.imageio.ImageIO;


import org.apache.commons.lang3.StringUtils;


import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;


public class PDFUtil {

// logger

private static final Logger lOGGER = LoggerFactory.getLogger(PDFUtil.class);


public static void main(String[] args) {

// pdfTojpg("C://Test//eClaimPDF//1//Others.pdf","C://Test//eClaimPDF//WrittenConfirmation.jpg");

Date timeDiffE = null;

Date timeDiffL = null;

timeDiffE = new Date();

base64PdfToJpg(pdfBase64);

timeDiffL = new Date();

lOGGER.info("base64PdfToJpg use time: " + getDiffTime(timeDiffL, timeDiffE) + "s");

}

private static String base64PdfToJpg(String base64Pdf) {

String jpg_base64 = null;

int pdfdpi = 400;

BASE64Decoder decoder = new BASE64Decoder();

byte[] pdf_bytes = null;

try {

pdf_bytes = decoder.decodeBuffer(base64Pdf);

} catch (IOException e1) {

e1.printStackTrace();

}


try (final PDDocument document = PDDocument.load(pdf_bytes)) {

int size = document.getNumberOfPages();

/*图像合并使用参数*/

// 定义宽度

int width = 0;

// 保存一张图片中的RGB数据

int[] singleImgRGB;

// 定义高度,后面用于叠加

int shiftHeight = 0;

//保存每张图片的像素值

BufferedImage imageResult = null;

// 利用PdfBox生成图像

PDDocument pdDocument = document;

PDFRenderer renderer = new PDFRenderer(pdDocument);

/*根据总页数, 按照50页生成一张长图片的逻辑, 进行拆分*/

// 每50页转成1张图片

int pageLength = size; //有多少转多少

// 总计循环的次数

int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;

for (int m = 0; m < totalCount; m++) {

for (int i = 0; i < pageLength; i++) {

int pageIndex = i + (m * pageLength);

if (pageIndex == pdDocument.getNumberOfPages()) {

System.out.println("m = " + m);

break;

}

// 96为图片的dpi,dpi越大,则图片越清晰,图片越大,转换耗费的时间也越多

BufferedImage image = renderer.renderImageWithDPI(pageIndex, 106, ImageType.RGB);

int imageHeight = image.getHeight();

int imageWidth = image.getWidth();

if (i == 0) {

//计算高度和偏移量

//使用第一张图片宽度;

width = imageWidth;

// 保存每页图片的像素值

// 加个判断:如果m次循环后所剩的图片总数小于pageLength,则图片高度按剩余的张数绘制,否则会出现长图片下面全是黑色的情况

if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {

imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);

} else {

imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);

}

} else {

// 将高度不断累加

shiftHeight += imageHeight;

}

singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);

imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);

}

// System.out.println("m = " + m);

File outFile = new File("C://Test//eClaimPDF//WrittenConfirmation.png");

System.out.println(outFile.getName());

// 写图片

ImageIO.write(imageResult, "png", outFile);

// 这个很重要,下面会有说明

shiftHeight = 0;

}

pdDocument.close();


ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流

ImageIO.write(imageResult, "png", baos);//写入流中

byte[] jpg_Bytes = baos.toByteArray();//转换成字节

BASE64Encoder encoder = new BASE64Encoder();

jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//转换成base64串

jpg_base64 = jpg_base64.replaceAll(" ", "").replaceAll(" ", "");//删除

// System.out.println("值为:"+"data:image/jpg;base64,"+jpg_base64);

} catch (Exception e) {

e.printStackTrace();

}

return "data:image/jpg;base64,"+jpg_base64;

}

// private static String base64PdfToJpg(String base64Pdf) {

// String jpg_base64 = null;

// int pdfdpi = 400;

//

// BASE64Decoder decoder = new BASE64Decoder();

// byte[] pdf_bytes = null;

// try {

// pdf_bytes = decoder.decodeBuffer(base64Pdf);

// } catch (IOException e1) {

// e1.printStackTrace();

// }

//

// try (final PDDocument document = PDDocument.load(pdf_bytes)) {

// int size = document.getNumberOfPages();

// for (int i = 0; i < size; i++) {

// BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);

// BufferedImage image = new PDFRenderer(document).

//

// ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流

// ImageIO.write(image, "jpg", baos);//写入流中

// byte[] jpg_Bytes = baos.toByteArray();//转换成字节

// BASE64Encoder encoder = new BASE64Encoder();

// jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//转换成base64串

// jpg_base64 = jpg_base64.replaceAll(" ", "").replaceAll(" ", "");//删除

//

// System.out.println("值为:"+"data:image/jpg;base64,"+jpg_base64);

//

// }

// } catch (Exception e) {

// e.printStackTrace();

// }

// return "data:image/jpg;base64,"+jpg_base64;

// }


private static void pdfTojpg(String pdfFilePath, String jpgFilePath) {

File pdfFile = new File(pdfFilePath);

int idx = jpgFilePath.lastIndexOf('.');

String jpgprefix = StringUtils.substring(jpgFilePath, 0, idx);

int pdfdpi = 400;

BASE64Decoder decoder = new BASE64Decoder();

byte[] bytes = null;

try {

bytes = decoder.decodeBuffer(pdfBase64);

} catch (IOException e1) {

e1.printStackTrace();

}


// try (final PDDocument document = PDDocument.load(pdfFile, "")) {

try (final PDDocument document = PDDocument.load(bytes)) {

int size = document.getNumberOfPages();

for (int i = 0; i < size; i++) {

BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);

/*

* ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流

* ImageIO.write(image, "jpg", baos);//写入流中 byte[] imgBytes =

* baos.toByteArray();//转换成字节 BASE64Encoder encoder = new BASE64Encoder();

* String png_base64 = encoder.encodeBuffer(imgBytes).trim();//转换成base64串

* png_base64 = png_base64.replaceAll(" ", "").replaceAll(" ", "");//删除

*

* System.out.println("值为:"+"data:image/jpg;base64,"+png_base64);

*/

File jpgFile = new File(jpgprefix + "_" + i + ".jpg");

ImageIO.write(image, "jpg", jpgFile);

}

} catch (Exception e) {

e.printStackTrace();

}


}

private static Double getDiffTime(Date lateTime, Date earlyTime) {

DecimalFormat df=new DecimalFormat("0.00");//设置保留位数

return Double.valueOf(df.format((double)(lateTime.getTime() - earlyTime.getTime()) / 1000));

}


public static String pdfBase64 = "***" //from web网页链接, upload a PDF to get the base64 string (Base64 - Online Base64 decoder and encoder)

}

⑨ java中如何用base64解码图片,并返回图片,不保存。

给你发个我以前的工具类吧、
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImageChange {
/**
* 从path这个地址获取一张图片然后转为base64码
* @param imgName 图片的名字 如:123.gif(是带后缀的)
* @param path 123.gif图片存放的路径
* @return
* @throws Exception
*/
public static String getImageFromServer(String imgName,String path)throws Exception{
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
File f = new File(path+imgName);
if(!f.exists()){
f.createNewFile();
}
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "gif", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
}

/**
* 将一个base64转换成图片保存在 path 文件夹下 名为imgName.gif
* @param base64String
* @param path 是一个文件夹路径
* @param imgName 图片名字(没有后缀)
* @throws Exception
*/
public static String savePictoServer(String base64String,String path,String imgName)throws Exception{
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream s = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(s);

Date timeCur = new Date();
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
String strYY = fmtYY.format(timeCur);
String strMM = fmtMM.format(timeCur);
String strDD = fmtDD.format(timeCur);

String realPath = path+"/"+strYY+"/"+strMM+"/"+strDD;

File dir=new File(realPath);
if(!dir.exists()){
dir.mkdirs();
}
String fileName=path+"\\"+strYY+"\\"+strMM+"\\"+strDD +"\\"+imgName+".gif";
File w2 = new File(fileName);//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动

return fileName;
}

public static void main(String[] args) throws Exception {
System.out.println(getImageFromServer("001001.gif","d:"));
}
}

⑩ 在java代码中怎么从服务器上把图片拿来放到数据库里

大概流程:
1.上传插件的选择:此篇博文选择的是jQuery的zyupload文件上传插件;
2.上传请求发起后,java代码的处理:你是要将上传的图片只保存在服务器还是只保存在数据库还是说两者都采取。上传到服务器很简单,保存到数据库也很简单,但是此处需要考虑业务,图片保存在数据库时采用哪种保存方式(本博文业务来自于项目,因为图片数量巨多,故在数据库是通过保存图片的路径实现的,并非二进制流);
3.图片保存在数据库后,在前段页面的回显功能。

阅读全文

与javabase64toimage相关的资料

热点内容
pdf手写笔 浏览:173
别永远伤在童年pdf 浏览:984
爱上北斗星男友在哪个app上看 浏览:414
主力散户派发源码 浏览:665
linux如何修复服务器时间 浏览:55
荣县优途网约车app叫什么 浏览:473
百姓网app截图是什么意思 浏览:222
php如何嵌入html 浏览:811
解压专家怎么传输 浏览:743
如何共享服务器的网络连接 浏览:132
程序员简易表白代码 浏览:167
什么是无线加密狗 浏览:63
国家反诈中心app为什么会弹出 浏览:68
cad压缩图打印 浏览:102
网页打开速度与服务器有什么关系 浏览:863
android开发技术文档 浏览:65
32单片机写程序 浏览:52
三星双清无命令 浏览:839
汉寿小程序源码 浏览:345
易助erp云服务器 浏览:533