Image图片转化为JPG图片

来源:互联网 发布:登录界面php代码 编辑:程序博客网 时间:2024/06/05 15:51

Image图片转化为JPG图片

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;

/**
 * @author Daniel Cao
 * @date 2007-5-18
 * @time 上午09:51:24
 *
 */
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格式的图片,原始图片可以是网路上的图片,也可以是本地磁盘上的图片。
 

另一种实现方式:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * @author Daniel Cao
 * @date 2007-5-18
 * @time 上午09:51:24
 *
 */
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",
     "jpg", "E:/111/aaa.png");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 输出图片只支持一些非标准的图片格式
  *
  * @param imageURL
  *            输入图片的URL完整路径,可以是本地文件,也可以是网路上的文件
  * @param type
  *            输出图片类型,只能是 bmp|png|jpg|jpeg
  * @param localPath
  *            保存在本地的路径
  * @throws Exception
  */
 public static void CopyImageFromURL(String imageURL, String type,
   String localPath) throws Exception {
  // 取得文件后缀名
  String tmpImageURL = imageURL.toLowerCase();
  String srcSuffix = tmpImageURL.substring(imageURL.lastIndexOf(".") + 1);
  String regex = "^gif|bmp|png|jpg|jpeg|jfif$";
  String regex1 = "^bmp|png|jpg|jpeg$";
  if (!srcSuffix.matches(regex)) {
   throw new Exception("不支持源图片格式[" + srcSuffix + "].");
  }
  if (!type.matches(regex1)) {
   throw new Exception("不支持输出图片格式[" + type + "].");
  }
  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 lastSep11 = localPath.lastIndexOf("/");
  int lastSep21 = localPath.lastIndexOf("//");
  int lastSep22 = (lastSep11 >= lastSep21 ? lastSep11 : lastSep21);
  String lastName = localPath.substring(lastSep22 + 1);
  boolean isOutputDir = true;
  if (lastName.indexOf(".") > 0) {
   int lst = lastName.lastIndexOf(".");
   String sfx = lastName.substring(lst + 1);
   if (sfx.matches(regex1)) {
    isOutputDir = false;
   }
  }
  String outputName;
  if (isOutputDir){//输出的是路径
   File localDir = new File(localPath);
   if (!localDir.exists()) {// 输入的是路径
    localDir.mkdirs();
   }
   //输出文件名从输入文件解析
   int lastSep1 = imageURL.lastIndexOf("/");
   int lastSep2 = imageURL.lastIndexOf("//");
   int lastSep = (lastSep1 >= lastSep2 ? lastSep1 : lastSep2);
   outputName = imageURL.substring(lastSep + 1);
  }else{//输出的是文件
   int lastSep1 = localPath.lastIndexOf("/");
   int lastSep2 = localPath.lastIndexOf("//");
   int lastSep = (lastSep1 >= lastSep2 ? lastSep1 : lastSep2);
   outputName = localPath.substring(lastSep + 1);
   String path = localPath.substring(0,lastSep);
   File outDir = new File(path);
   if (!outDir.exists()){
    outDir.mkdirs();
   }
   localPath = path;
  }
  //用给定的后缀输出
  String newName = outputName.substring(0,outputName.lastIndexOf(".")) + "." + type;
  FileOutputStream out = new FileOutputStream(localPath + "/" + newName);
  ImageIO.write(src, type, out);
  System.out.println(localPath + "/" + newName + "输出成功.");

  out.close();
 }

 

原创粉丝点击