圖片轉base64java
㈠ 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中如何用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 base64
java base64是什麼,讓我們一起了解一下?
Base64是一種編碼方法,要求把每三個8Bit的位元組轉換為四個6Bit的位元組,轉換之後的這四個位元組中每6個晌慧穗有效bit為有效數據,空餘的那兩個bit用0補上成為一個位元組。
為什麼要使用Base64?
Base 64主要用途不是加密,而是把一些二進制數轉成普通字元,方便在網路上傳輸。 由於一些二進制字元在傳輸協議中屬於控制字元,不能直接傳送,所以需要轉換一下才可以。由於某些系統中只能使用ASCII字元,Base64就是用來將非ASCII字元的數據轉換成ASCII字元的一種方法,Base64特別適合在http,mime協議下快速傳輸數據。
比如網路中圖片的傳輸Base64,並非安全領域下的加密解密演算法。雖然經常遇到所謂的base64的加密解密。但base64隻能算是一個編碼演算法,對數據內容進行編碼來適合傳輸。雖然base64編碼過後原文也變成不能看到的字元格式,但是方式初級又簡單。
那在Java中想要實現Base64的加解密,有哪些方式?
主要有以下四種(推薦度由低到高):
1、JDK中的宴卜sun.misc套碧桐件。
2、第三方擴展包 bouncy castle。
3、第三方擴展包 commons codec。
4、JDK8及更高版本中的 java.util.Base64。
實戰操作,JDK實現代碼如下: import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * BASE64加密解密 */ public class BASE64 { /** * BASE64解密 * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } public static void main(String[] args) throws Exception { String data = BASE64.encryptBASE64("http://aub.iteye.com/".getBytes()); System.out.println("加密前:" + data); byte[] byteArray = BASE64.decryptBASE64(data); System.out.println("解密後:" + new String(byteArray)); } }
㈣ Java 保存圖片到資料庫時,為什麼要對圖片進行base64編碼
首先這是一種SB做法,圖片保存到資料庫這個很浪費資料庫資源, 通常情況下圖片等文件都是用ftp伺服器來存儲文件的. 為什麼要用base64進行編碼是因為, base64會把文件這個文件轉換成字元串, base64編碼後得到的是一組字元串, 為什麼要用blob類型, 因為這個類型可以存儲4GB數據, 資料庫中普通的 varchar varchar2 text等類型都有長度的限制
㈤ java讀取照片保存到達夢資料庫
存儲圖片是後端伺服器比較基礎的功能,一般來說,圖片可以存儲在伺服器的文件系統中,然後資料庫中只需要存儲url就可以了。另外一種辦法是,將圖片通過Base64編碼後存儲到資料庫中,資料庫中存儲圖片的base64編碼的二進制可以使用TEXT(mysql)類型。
㈥ android 圖片轉BASE64上傳提示java.lang.OutOfMemoryError
我最近也碰到了這個問題,但是網上沒有找到相關有效的直接解決方法。
後來看到了一篇解釋base64編碼原理的文章,研究了一番後解決了。
一般碰到這個問題的,都涉及到"大文件上傳"的問題,"大文件上傳"過程中除了base64編碼時可能OOM,其實還有其他問題,雖然提問中沒有提出,可能是因為這個問題還沒有解決,所以還沒有遇到其它問題,我就圍繞著"大文件上傳"來解決這個問題吧。
(提問時間在下看的清楚)
————————————————————
做項目的過程中碰到一個需求:
在java客戶端,使用http通信,把客戶端的本地文件通過http發送上傳到伺服器;
請求格式是xml(不管是json還是xml都是字元串,所以這個無所謂),中間包含[文件流字元串];
之前的做法是,把文件流通過base64編碼轉換為base64Byte,然後和其它字元串信息放到一起,post的時候通過HttpURLConnection的write方法寫入到伺服器中去,這個上傳的過程就完成了。
——————————
但是碰到一個問題,當文件體積較大時,從文件流轉換成base64Byte後,體積會很大,可能會導致OOM;
(以二進制流的方式保存,體積最小;以byte數組的方式保存,體積會相對變大一些;以String形式保存,體積最大;)
出錯原因是:
FileInputStream fis = new FileInputStream(file); //這一步打開了一個對准file准備進行讀取的文件指針,但是還沒有開始讀寫,file的相關數據沒有從本地載入到內存中來;所以即使file的體積有10G那麼大,這一步也是不會OOM的
//把文件流轉換為位元組數組
byte[] fileBytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteBuf = new byte[1024];
int count;
while((count=fis.read(buf))!=-1)
{
baos.write(buf,0,count); //實際上,如果文件體積比較大的話,不用轉碼,在這一步就可能OOM了
}
fileBytes= baos.toByteArray();
byte[] base64Bytes = Base64.encodeBase64(fileBytes); //在這一步也可能OOM
(文件轉換為byte[]時,是有可能OOM的;而轉換為base64Bytes後,體積會增大1/3,所以有可能前一步沒有OOM,卻在這一步出現OOM;
為什麼轉碼後體積會增大1/3,後面我會解釋)
——————————
解決方法
既然file在本地沒有載入到內存來的時候不會出現內存溢出的情況,我就想到了一個解決的方法:分段上傳
(加大內存並不能從根本上解決內存溢出的問題,問題的根本原因不是內存不夠大,而是代碼有問題)
在本地的file通過HttpURLConnection的getOutputStream()進行write時,不是一次性全部寫入,而是循環配合flush進行寫入:
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int count;
while((count = fis.read(buf)) != -1)
{
os.write(Base64.encodeBase64(buf), 0, count);
os.flush();
}
(我從本地讀1024位元組,然後馬上上傳到伺服器,清空本地緩存,然後再從本地讀1024位元組,這樣循環讀取,即使文件有20G,理論上也不有OOM問題出現,因為我從本地文件中讀到的數據不會在內存中駐留)
——————————
解決問題的思路對了,但是出現了其他的細節問題
os.write(Base64.encodeBase64(buf), 0, count); //這一行代碼報錯了,出現了OOM
我搜集了一下資料,發現原因是:
HttpURLConnection的getOutputStream的實際對象是sun.net.<a href="http://www.http.PosterOutputStream" target="_blank">www.http.PosterOutputStream</a>,這個對象的flush方法代碼是空的,write配合flush,並沒有達到即時上傳數據的效果。PosterOutputStream其實是自己在本地維護了一個緩沖區,你通過write寫入的數據其實還是在這個本地的緩沖區里,只有當你getInputStream後,HttpURLConnection才會把這段緩沖區中的數據上傳到伺服器上。而flush達不到上傳數據,清空本地緩存的效果。
——————————
(我是不能通過getInputStream來刷新緩沖流的,因為那就不是分段上傳而是"分次"上傳了)
那這就不是我的思路的問題了。再去搜索解決方法後,得知:
在創建HttpURLConnection對象的時候,要調用一個方法
hurlc.setChunkedStreamingMode(1024); //設置分塊流模式 也就是分塊上傳 1024是getOutputStream維護的本地緩沖區的大小
調用該方法後,只要本地緩存區滿了,HttpURLConnection就會自動把緩沖區里的數據發送到伺服器,同時清空本地緩存(ps:HttpURLConnection的getOutputStream似乎是個抽象的工廠方法,在調用setChunkedStreamingMode方法後,我發現getOutputStream獲取到的實例對象從sun.net.<a href="http://www.http.PosterOutputStream" target="_blank">www.http.PosterOutputStream</a>變成了sun.net.<a href="http://www.protocol.http.HttpURLConnection$StreamingOutputStream" target="_blank">www.protocol.http.HttpURLConnection$StreamingOutputStream</a>)
——————————
果然,調用setChunkedStreamingMode方法後,os.write(Base64.encodeBase64(buf), 0, count);沒有再出現OOM異常了
但是,又出現了一個新的問題
我發現
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int count;
while((count = fis.read(buf)) != -1)
{
os.write(Base64.encodeBase64(buf), 0, count);
os.flush();
}
這段分段編碼寫入的代碼,其編碼所得結果,與非分段編碼所得結果是不一樣的
通過分段編碼上傳的圖片內容出現了錯誤
我通過下面代碼測試:
//分段編碼
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] buf1 = new byte[1024];
int count1;
while((count1 = file1.read(buf1)) != -1)
{
os1.write(Base64.encodeBase64(buf1), 0, count1);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
//非分段編碼
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
InputStream file2 = new FileInputStream(path);
byte[] buf2 = new byte[1024];
int count2;
while((count2 = file2.read(buf2)) != -1)
{
os2.write(buf2, 0, count2);
os2.flush();
}
file2.close();
System.out.println(new String(Base64.encodeBase64(os2.toByteArray())));
兩者的結果:
/9j/4AAQSkZJR...wDtUAVs7eF...
/9j/4AAQSkZJR...wDt89ymnxJ...
前面一段還是相同的,轉到後面,就開始南轅北轍了
——————————
原因我去網上找了一下但是沒有找到直接答案,但是看到一篇解釋base64編碼原理的文章
原文鏈接:<a href="http://www.cnblogs.com/luguo3000/p/3940197.html" target="_blank">http://www.cnblogs.com/luguo3000/p/3940197.html</a>
假設有文件A(txt文件,包含文本內容"ABCDEFG"),轉換為InputStream->byte[]後
它們的ASIIC碼分別對應65、66、67、68、69、70、71
二進製表現形式為:
1000001 1000010 1000011 1000100 1000101 1000110 1000111
對高位補零後:
01000001 01000010 01000011 01000100 01000101 01000110 01000111
在內存中的實際表現:
而base64編碼,使用的字元包括(A-Z、a-z、0-9、+、/、=)這些常規可讀字元,使用base64編碼的原因,用途,在於把一些亂碼字元、不可讀字元轉換為常規可讀字元;
(因為java底層的通信協議、或者說其它的通信協議,很多地方用到遠程通信這一塊的,對一些亂碼字元不支持傳輸,所以需要把亂碼字元轉換成常規可讀字元才能進行傳輸)
比如對於'矙'這個字元,部分傳輸協議的編碼集就不認識它,所以無法直接傳輸,必須base64轉碼
'矙'的UTF-8編碼值為30681,二進製表現形式為111011111011001->(0)111011111011001
需要兩個位元組來存儲01110111 11011001
base64編碼只有(A-Z、a-z、0-9、+、/、=)這些字元來表示。需要強調的是,在base64編碼規范中,字元'A'不等於65、'B'也不是66...。base64字元與數值(二進制值)的對應關系如下:
也就是說,常規字元'A'=65=01000001;而base64字元'A'=0=00000000;
base64字元代表的二進制值是無法直接表示'矙'這個字元的,因為base64字元的值范圍在0~63之間(二進制值在(00)000000~(00)111111之間)。
那如何通過(00)000000~(00)111111之間的數值來表示01110111 11011001呢?
這就是base64的編碼演算法了
一個base64字元的二進制值在(00)000000~(00)111111之間,也就是說它可以表示000000~111111之間的二進制數,一個base64字元的有效位為後6位。如何通過以6bit為單位的base64字元表示以8bit為單位的常規位元組?
6和8的最小公倍數為24,即 每4個base64字元可以表示3個常規位元組;
回到剛才的文件A,編碼過程:
(初始文件A)->"ABCDEFG"
(轉UTF-8碼 int)->65 66 67 68 69 70 71
("ABCDEFG"的二進製表示;7位元組)->1000001 1000010 1000011 1000100 1000101 1000110 1000111
(高位補零)->01000001 01000010 01000011 01000100 01000101 01000110 01000111
(連寫)->
(按6bit為單位對所有bit進行分割;得到10位元組)->010000 010100 001001 000011 010001 000100 010101 000110 010001 11
(按6bit*4=8bit*3的對應關系再分割;得到3組6*4位元組)->(010000 010100 001001 000011) (010001 000100 010101 000110) (010001 11)
(高位補2個零;末尾的低位也補零)->(00010000 00010100 00001001 00000011) (00010001 00000100 00010101 00000110) (00010001 00110000)
(二進制值換算成十進制)->(16 20 9 3) (17 4 21 6) (17 48)
(按base64編碼的值-字元對應表,得出上面的十進制值對應的base64字元)->(Q U J D) (R E V G) (R w)
(每組base64字元都要求是4個,空白的位置補'='字元)->(Q U J D) (R E V G) (R w = =)
(文件A的最終轉碼結果)->QUJDREVGRw==
這里以文本文件作為演示,因為文本文件機器可讀人也可讀;實際情況中,很多時候轉碼的目標文件並不是文本文件,那就不能以可讀字元串形式表示了,會直接以二進制格式表示
體積增大的原因,是因為3位元組=24bit=分割成4個6bit-,對4個6bit高位補零後,就得到4個位元組
也就是說3個常規位元組經base64編碼後會生成4個base64位元組,這就是文件經base64轉碼後體積會增加1/3的原因
——————————
base64編碼原理解釋了,再看剛才的分段編碼
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] buf1 = new byte[1024];
int count1;
while((count1 = file1.read(buf1)) != -1)
{
os1.write(Base64.encodeBase64(buf1), 0, count1); //可以發現一個問題:Base64.encodeBase64(buf1)編碼後,體積會增加1/3,所以這里的Base64.encodeBase64(buf1)編碼轉換後的實際長度和count1並不相等,所以實際寫入到os1中的base64字元數只有Base64.encodeBase64(buf1)編碼產生的字元數的3/4
os1.flush();
}
file1.close();
System.out.println(os1.toString());
修改後:
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] byteBuf = new byte[1024];
byte[] base64ByteBuf;
while(file1.read(byteBuf) != -1)
{
base64ByteBuf = Base64.encodeBase64(byteBuf);
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
——————————
修改後,發現分段編碼的結果發生了變化,跟之前不一樣了
但仍然不是正確的結果
原因在於,base64字元的基礎單位是(6bit*4=4位元組),而3個常規位元組(8bit*3)才能剛好產生4個base64位元組
根本原因在於,如果進行編碼的常規位元組數不是3的倍數,最後就會餘下1或2個位元組,而這1、2個位元組編碼的結果就會產生'='字元;
使用1024作為分段編碼緩沖時,編碼的結果是3+3+3+...+1
也就是每次都會餘1位元組
而沒有使用分段編碼時,當編碼到第1024個位元組時,"餘下"的1位元組會跟後面的位元組形成連續,就不會產生'='字元
(對一段byte字元進行base64編碼時,中間是絕不會產生'='字元的,因為只有在結尾才可能餘下1或2個位元組,所以對一段byte字元進行編碼時,只有結尾才可能產生1或2個'='補全字元)
——————————
解決方法是,使用3的公倍數作為緩沖區大小
修改後:
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] byteBuf = new byte[3*1000];
byte[] base64ByteBuf;
while(file1.read(byteBuf) != -1)
{
base64ByteBuf = Base64.encodeBase64(byteBuf);
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
測試結果再次發生了改變
中間不再有'='字元了,因為中間每次都是3位元組3位元組的編碼,沒有餘下多餘的位元組
對比之後發現,中間段的結果已經正常了
——————————
但是,發現,結尾處兩個轉碼的結果有些許不同
原因在於,假設文件A的長度為3001個位元組;
在第二次循環讀取時,只讀到1個有效位元組,而byteBuf的剩餘2999個位元組都是無效位元組,而此時編碼時,卻把多餘的2999個無效位元組也編碼了進去
(如果是非分段轉碼,就不會出現這種情況)
解決方法:
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
InputStream file1 = new FileInputStream(path);
byte[] byteBuf = new byte[3*1000];
byte[] base64ByteBuf;
int count1; //每次從文件中讀取到的有效位元組數
while((count1=file1.read(byteBuf)) != -1)
{
if(count1!=byteBuf.length) //如果有效位元組數不為3*1000,則說明文件已經讀到尾了,不夠填充滿byteBuf了
{
byte[] = Arrays.Of(byteBuf, count1); //從byteBuf中截取包含有效位元組數的位元組段
base64ByteBuf = Base64.encodeBase64(); //對有效位元組段進行編碼
}
else
{
base64ByteBuf = Base64.encodeBase64(byteBuf);
}
os1.write(base64ByteBuf, 0, base64ByteBuf.length);
os1.flush();
}
file1.close();
System.out.println(os1.toString());
至此,base64分段編碼才算大功告成。大文件上傳核心代碼才算大功告成。
其實代碼改起來非常簡單,但是不知道原因不知道原理的話,是無法無中生有的
對我本人來說原本只是想隨便答一下,但沒想到答的過程中發現自己有很多坑沒有發現。答的過程中把自己不懂的地方沒有發現的坑也完善了。不說碰到一個知識點就要追根究底,但實際開發中,每一個自己能親身碰到的實際問題都是鍛煉自己的絕佳機會,這種近距離觸碰問題、解決問題的機會是難得的。雖然開發中還有很多其它問題也很重要,但你沒有親手碰到過,是無法共鳴的。所以自己在開發中碰到了問題,還是建議大概弄清原因。
弄清原理後,即使以後出現這個問題的其它"變種",也能找到原因並自己解決,但僅僅粘貼復制無法做到這一點。
㈦ java 把一個網路圖片轉換為base64
這個簡單啊
(1)把獲取url流轉為乎廳bitmap
(2)把歲滲隱bitmap再轉為base64
public static Bitmap getBitMBitmap(String urlpath) {
Bitmap map = null;
try {
URL url = new URL(urlpath);
URLConnection conn = url.openConnection();
conn.connect();
InputStream in;
in = conn.getInputStream();
map = BitmapFactory.decodeStream(in);
/喊數/ TODO Auto-generated catch block
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
第二步
/**
* bitmap轉為base64
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
有什麼問題提問就好
㈧ 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壓縮至32K以下後的圖片base64碼
Java實現圖片與Base64編碼互轉
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;
publicclassBase64Image{
publicstaticvoidmain(String[]args){
局游//測試從Base64編碼轉換為圖片文件
StringstrImg="自己寫哈";
GenerateImage(strImg,"D:\wangyc.jpg");
//測試從圖片文件轉換為Base64編碼
System.out.println(GetImageStr("d:\wangyc.jpg"));
}
publicstaticStringGetImageStr(StringimgFilePath){//將圖片文件轉化為位元組數組字元串,並對其進行Base64編碼處理
byte[]data=null;
//讀取圖片位元組數組
try{
InputStreamin=newFileInputStream(imgFilePath);
data=newbyte[in.available()];
in.read(data);
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
//對位元組數組Base64編碼
BASE64Encoderencoder=newBASE64Encoder();
returnencoder.encode(data);//返回Base64編碼過的位元組數組字元串
}
(StringimgStr,StringimgFilePath){//對位元組數組字元串進行Base64解碼並生成圖片
if(imgStr==null)//圖像數據為空
returnfalse;
BASE64Decoderdecoder=newBASE64Decoder();
try{
//Base64解碼
byte[]前陵bytes=decoder.decodeBuffer(imgStr);
for(inti=0;i<bytes.length;++i){
if(bytes[i]<0){//調整異常數據
bytes[i]+=256;
}
}
//生成jpeg圖片
OutputStreamout=newFileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
桐悔銷returntrue;
}catch(Exceptione){
returnfalse;
}
}
}
㈩ java和asp.net 分別將同一張圖片轉換成base64位後,大小不一樣,無法通用
要使用Oracle資料庫實例,有兩種方式來實現。
一個是使用CLOB欄位Base64編碼後的圖像存儲,WEB應用訪問欄位(String對象),使用Base64編碼,然後輸出。
有一個二進制大對象欄位使用的Blob,直接存儲對象位元組流可以是任何對象,如圖片,視頻,文檔等,然後WEB應用越來越重最初建為一個位元組Blob對象流對象。
但無論哪種方式在非洲的業務發展是真的有必要在此情況下,絕對不推薦,因為數據是更大的訪問資料庫悄喚的性能開銷更高的應用效率會比較低後續系統的SQL腳本不能只通過開展,由Oracle DMP地的。因為無論是長,CLOB或BLOB,不是一個簡單的SQL插入,推薦的做法是存儲文件資料庫VARCHAR2領域的WEB容器相對路徑(啟凳凱圖片,粗春視頻,文檔等),WEB應用程序只能通過需要訪問鏈接的對象的路徑。