java 怎么 png转jpg java 怎么 png转jpg 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.net.URL;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageUtil { /** * @param args */ public static void main(String[] args) { try{ CopyImageFromURL("E:/cat.png","E:/111/aaa.jpg"); CopyImageFromURL("http://www.csdn.net/ui/styles/public_header_footer/logo_csdn.gif","E:/111/logo_csdn.jpg"); }catch(Exception e){ e.printStackTrace(); } } /** * * @param imageURL 输入图片的URL完整路径,可以是本地文件,也可以是网路上的文件 * @param localPath 保存在本地的路径 * @throws Exception */ public static void CopyImageFromURL(String imageURL, String localPath) throws Exception { // 取得文件后缀名 String tmpImageURL = imageURL.toLowerCase(); String regex = "(\\.)(gif|bmp|png|dib|jpg|jpeg|jfif)"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(tmpImageURL); if (!matcher.find()) { throw new Exception("不支持该格式文件."); } URL url; if (imageURL.toLowerCase().startsWith("http")) {// http url url = new URL(imageURL); } else {// 文件url String tmpUrl = imageURL.toLowerCase(); String beginer = tmpUrl.split("/")[0]; if (beginer.matches("[c-o]:")) {// 路径支持到c:d:....o: url = new File(imageURL).toURL(); } else { throw new Exception("无法解析文件URL."); } } BufferedImage src = ImageIO.read(url); int width = src.getWidth(null); int height = src.getHeight(null); // 保存图像到本地 int lastSep1 = localPath.lastIndexOf("/"); int lastSep2 = localPath.lastIndexOf("\\"); int lastSep = (lastSep1>=lastSep2?lastSep1:lastSep2); String path = localPath.substring(0,lastSep); File localDir = new File(path); if (!localDir.exists()) { localDir.mkdirs(); } BufferedImage outImg = new BufferedImage(width, height,src.getType()); outImg.getGraphics().drawImage(src, 0, 0, width, height, null); FileOutputStream out = new FileOutputStream(localPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(outImg); out.close(); }} 上面这段代码可以把支持的所有格式图片gif|bmp|png|dib|jpg|jpeg|jfif转化为jpg/jpeg格式的图片 RenderedImage img = ImageIO.read(new File("a.png")); ImageIO.write(img, "jpg", new File("a.jpg")); RenderedImage img = ImageIO.read(new File("a.png")); ImageIO.write(img, "jpg", new File("a.jpg"));-----------------------轻松的满足了LZ的需求 楼上正解!这样才是最简单的,com.sun包是低层API,一般情况下不提倡使用这个包.com.sun.image.codec.jpeg.JPEGCodec;com.sun.image.codec.jpeg.JPEGImageEncoder;已经在javax.ImageIO.ImageIO里使用.不用我们操心它们. 如何执行远程计算机上的一个程序 问:怎么用java以树形来显示出一个table(菜单?) 一个关于JAVA正则表达式的小问题 泛型问题 学习static遇到个问题? JDBC executeQuery()方法在执行过程中,与数据库服务器的connection物理断开了,如何去捕捉到呢? 关于java计算的精度问题,求助 谁有JCrector Pro的中文教材? 我没有java做项目的思路,请大神帮忙,真心急 一个奇怪的问题。看一个小程序。 考题:求1到n之间能被5整除的所有数的乘积可以用递归 为什么swing的菜单弹出速度没有win32的快,总是要停顿一下?
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageUtil { /**
* @param args
*/
public static void main(String[] args) {
try{
CopyImageFromURL("E:/cat.png","E:/111/aaa.jpg");
CopyImageFromURL("http://www.csdn.net/ui/styles/public_header_footer/logo_csdn.gif","E:/111/logo_csdn.jpg");
}catch(Exception e){
e.printStackTrace();
}
} /**
*
* @param imageURL 输入图片的URL完整路径,可以是本地文件,也可以是网路上的文件
* @param localPath 保存在本地的路径
* @throws Exception
*/
public static void CopyImageFromURL(String imageURL, String localPath)
throws Exception {
// 取得文件后缀名
String tmpImageURL = imageURL.toLowerCase();
String regex = "(\\.)(gif|bmp|png|dib|jpg|jpeg|jfif)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(tmpImageURL);
if (!matcher.find()) {
throw new Exception("不支持该格式文件.");
}
URL url;
if (imageURL.toLowerCase().startsWith("http")) {// http url
url = new URL(imageURL);
} else {// 文件url
String tmpUrl = imageURL.toLowerCase();
String beginer = tmpUrl.split("/")[0];
if (beginer.matches("[c-o]:")) {// 路径支持到c:d:....o:
url = new File(imageURL).toURL();
} else {
throw new Exception("无法解析文件URL.");
}
}
BufferedImage src = ImageIO.read(url);
int width = src.getWidth(null);
int height = src.getHeight(null);
// 保存图像到本地
int lastSep1 = localPath.lastIndexOf("/");
int lastSep2 = localPath.lastIndexOf("\\");
int lastSep = (lastSep1>=lastSep2?lastSep1:lastSep2);
String path = localPath.substring(0,lastSep);
File localDir = new File(path);
if (!localDir.exists()) {
localDir.mkdirs();
}
BufferedImage outImg = new BufferedImage(width, height,src.getType());
outImg.getGraphics().drawImage(src, 0, 0, width, height, null);
FileOutputStream out = new FileOutputStream(localPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(outImg); out.close();
}
}
ImageIO.write(img, "jpg", new File("a.jpg"));
ImageIO.write(img, "jpg", new File("a.jpg"));
-----------------------
轻松的满足了LZ的需求
com.sun.image.codec.jpeg.JPEGCodec;
com.sun.image.codec.jpeg.JPEGImageEncoder;
已经在javax.ImageIO.ImageIO里使用.不用我们操心它们.