① 圖片經由過程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.圖片保存在資料庫後,在前段頁面的回顯功能。