Java画像変換フォーマット

15643 ワード

Java画像変換フォーマット
  • 参照
  • コード
  • ネット上のコードを参考にして、記録します.リンク画像変換を追加
    リファレンス
    JAVA万能画像フォーマット変換
    コード#コード#
    ImageConvert.javaピクチャフォーマット変換ツールクラス
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    import javax.imageio.ImageIO;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class ImageConvert {
    	private static BASE64Encoder encoder = new BASE64Encoder();
    	private static BASE64Decoder decoder = new BASE64Decoder();
    
    	/**
    	 *       (    )
    	 * 
    	 * @param sourceFileName
    	 *                 
    	 * @param newFileName
    	 *                    
    	 */
    	public static void convertLocalImageFormat(String sourceFilePath, String newFilePath) {
    		try {
    			String base64String = getImageBinary(sourceFilePath);
    			byte[] bytes1 = decoder.decodeBuffer(base64String);
    			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes1);
    			imageConvertFormat(inputStream, newFilePath);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 *       (    )
    	 * 
    	 * @param sourceFileName
    	 *                 
    	 * @param newFileName
    	 *                    
    	 */
    	public static void convertUrlImageFormat(String urlStr, String newFilePath) {
    		try {
    			URL url = new URL(urlStr);
    			//     
    			URLConnection con = url.openConnection();
    			//        5s
    			con.setConnectTimeout(5 * 1000);
    			//    
    			InputStream is = con.getInputStream();
    			imageConvertFormat(is, newFilePath);
    		} catch (MalformedURLException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 *       
    	 * 
    	 * @param is
    	 *               
    	 * @param newFilePath
    	 *                    
    	 */
    	public static void imageConvertFormat(InputStream is, String newFilePath) {
    		try {
    			BufferedImage bi1 = ImageIO.read(is);
    			File w2 = new File(newFilePath);//    jpg,png,gif  
    			ImageIO.write(bi1, getFileType(newFilePath), w2);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 *       ,     
    	 * 
    	 * @param fileName
    	 *               
    	 * @return String     
    	 */
    	private static String getFileType(String fileName) {
    		return fileName.substring(fileName.indexOf(".") + 1, fileName.length());
    	}
    
    	/**
    	 *       base64   
    	 * 
    	 * @param fileName
    	 *               
    	 * @return String base64   
    	 */
    	private static String getImageBinary(String fileName) {
    		File f = new File(fileName);
    		BufferedImage bi;
    		try {
    			bi = ImageIO.read(f);
    			String fileType = getFileType(fileName);
    			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    			ImageIO.write(bi, fileType, outputStream);
    			byte[] bytes = outputStream.toByteArray();
    
    			return encoder.encodeBuffer(bytes).trim();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    }