[英]compress base 64 png image in java
嗨,我想知道Java中是否有減小圖像大小的方法。實際上,我的前端是IOS,它們正在發送Base 64編碼數據,並且當我獲取編碼數據並解碼時編碼數據並存儲在字節數組中。 現在我想在Java和我的方法代碼中壓縮PNG圖片
public String processFile(String strImageBase64, String strImageName,String donorId)
{
FileOutputStream fos =null;
File savedFile=null;
try
{
String FileItemRefPath = propsFPCConfig.getProperty("fileCreationReferencePath");
String imageURLReferncePath = propsFPCConfig.getProperty("imageURLReferncePath");
File f = new File(FileItemRefPath+"\\"+"productimages"+"\\"+donorId);
String strException = "Actual File "+f.getName();
if(!f.exists())
{
boolean isdirCreationStatus = f.mkdir();
}
String strDateTobeAppended = new SimpleDateFormat("yyyyMMddhhmm").format(new Date(0));
String fileName = strImageName+strDateTobeAppended;
savedFile = new File(f.getAbsolutePath()+"\\"+fileName);
strException=strException+" savedFile "+savedFile.getName();
Base64 decoder = new Base64();
byte[] decodedBytes = decoder.decode(strImageBase64);
if( (decodedBytes != null) && (decodedBytes.length != 0) )
{
System.out.println("Decoded bytes length:"+decodedBytes.length);
fos = new FileOutputStream(savedFile);
System.out.println(new String(decodedBytes) + "\n") ;
int x=0;
{
fos.write(decodedBytes, 0, decodedBytes.length);
}
fos.flush();
}
//System.out.println(savedFile.getCanonicalPath() +" savedFile.getCanonicalPath() ");
if(fos != null)
{
fos.close();
return savedFile.getAbsolutePath();
}
else
{
return null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if( fos!= null)
{
fos.close();
}
else
{
savedFile = null;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return savedFile.getName();
}
並且我正在使用圖像名稱存儲此解碼數據,現在我想將此壓縮圖像存儲在另一個網址中
我認為這不值得付出努力。
PNG已經具有很高的壓縮率。 很難通過額外的壓縮來減小尺寸。
如果您確實要向客戶端發送圖像或編碼為Base64的響應,那么當然可以使用一些方法來提高傳輸速率:在服務器上啟用gzip壓縮,以便對HTTP響應進行gzip壓縮。 萬一原始數據經過Base64編碼(這實際上意味着您僅使用每字節8位中的6位),這將減少實際傳輸字節的數量。 啟用gzip壓縮對您的服務器代碼是透明的,並且對於大多數Web服務器而言只是一個配置開關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.