1. 程式人生 > >dicom之將dcm檔案解析為jpg後等比例壓縮

dicom之將dcm檔案解析為jpg後等比例壓縮

前段時間用dcm4chee對dcm檔案進行了轉換,轉換格式為bmp和jpg,但是轉化完後發現6M的dcm原始檔變成了一個13M的bmp,手機端顯示會很吃力,於是決定做個壓縮處理,提供兩種壓縮方式,第一種方式的壓縮時間會比較長,第二種就比較好:code1:
package com.ifly.dicom;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
 * 
 * ClassName: ImageCondenseUtil 
 * @Description: TODO
 * @author wangyl
 * @date 2017年6月16日
 */
public class ImageCondenseUtil {
	/**
	 * 採用指定寬度、高度或壓縮比例 的方式對圖片進行壓縮
	 * 
	 * @param imgsrc
	 *            源圖片地址
	 * @param imgdist
	 *            目標圖片地址
	 * @param widthdist
	 *            壓縮後圖片寬度(當rate==null時,必傳)
	 * @param heightdist
	 *            壓縮後圖片高度(當rate==null時,必傳)
	 * @param rate
	 *            壓縮比例
	 */
	public void reduceImg(String imgsrc, String imgdist, int widthdist,
			int heightdist, Float rate) {
		try {
			File srcfile = new File(imgsrc);
			// 檢查檔案是否存在
			if (!srcfile.exists()) {
				return;
			}
			// 如果rate不為空說明是按比例壓縮
			if (rate != null && rate > 0) {
				// 獲取檔案高度和寬度
				int[] results = getImgWidth(srcfile);
				if (results == null || results[0] == 0 || results[1] == 0) {
					return;
				} else {
					widthdist = (int) (results[0] * rate);
					heightdist = (int) (results[1] * rate);
				}
			}
			// 開始讀取檔案並進行壓縮
			Image src = javax.imageio.ImageIO.read(srcfile);
			BufferedImage tag = new BufferedImage((int) widthdist,
					(int) heightdist, BufferedImage.TYPE_INT_RGB);

			tag.getGraphics().drawImage(
					src.getScaledInstance(widthdist, heightdist,
							Image.SCALE_SMOOTH), 0, 0, null);

			FileOutputStream out = new FileOutputStream(imgdist);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(tag);
			out.close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 獲取圖片寬度
	 * 
	 * @param file
	 *            圖片檔案
	 * @return 寬度
	 */
	public static int[] getImgWidth(File file) {
		InputStream is = null;
		BufferedImage src = null;
		int result[] = { 0, 0 };
		try {
			is = new FileInputStream(file);
			src = javax.imageio.ImageIO.read(is);
			result[0] = src.getWidth(null); // 得到源圖寬
			result[1] = src.getHeight(null); // 得到源圖高
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		ImageCondenseUtil util =new ImageCondenseUtil();
		System.out.println("開始壓縮:" + new Date().toLocaleString());
		File srcfile = new File("C:\\Users\\qxb-810\\Desktop\\pacs\\DICOM\\pic");
		File[] files =srcfile.listFiles();
		for(File file:files){
			util.reduceImg(file.getAbsolutePath(),file.getAbsolutePath(), 400, 400, null);
		}
		System.out.println("結束壓縮:" + new Date().toLocaleString());

	}

}

code2:



package com.ifly.dicom;

import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.*;
/**
 * 
 * ClassName: ImgCompress 
 * @Description: 影象壓縮處理
 * @author wangyl
 * @date 2017年6月16日
 */
public class ImgCompress {
	private Image img;
	private int width;
	private int height;
	private File destFile;
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception {
		System.out.println("開始:" + new Date().toLocaleString());
		String inPath = "C:\\Users\\qxb-810\\Desktop\\pacs\\DICOM\\pic";
		File filesFile = new File(inPath);
		File[] files = filesFile.listFiles();
		for(File file: files){
			ImgCompress imgCom = new ImgCompress(file);
			imgCom.resizeFix(400, 400,file);
		}
		System.out.println("結束:" + new Date().toLocaleString());
	}
	/**
	 * 建構函式
	 */
	public ImgCompress(File file) throws IOException {
		img = ImageIO.read(file);      // 構造Image物件
		width = img.getWidth(null);    // 得到源圖寬
		height = img.getHeight(null);  // 得到源圖長
		destFile =file;  //輸出路徑
	}
	/**
	 * 按照寬度還是高度進行壓縮
	 * @param w int 最大寬度
	 * @param h int 最大高度
	 */
	public void resizeFix(int w, int h,File outFile) throws IOException {
		if (width / height > w / h) {
			resizeByWidth(w);
		} else {
			resizeByHeight(h);
		}
		
	}
	/**
	 * 以寬度為基準,等比例放縮圖片
	 * @param w int 新寬度
	 */
	public void resizeByWidth(int w) throws IOException {
		int h = (int) (height * w / width);
		resize(w, h,destFile);
	}
	/**
	 * 以高度為基準,等比例縮放圖片
	 * @param h int 新高度
	 */
	public void resizeByHeight(int h) throws IOException {
		int w = (int) (width * h / height);
		resize(w, h,destFile);
	}
	/**
	 * 強制壓縮/放大圖片到固定的大小
	 * @param w int 新寬度
	 * @param h int 新高度
	 */
	public void resize(int w, int h,File destFile) throws IOException {
		// SCALE_SMOOTH 的縮略演算法 生成縮圖片的平滑度的 優先順序比速度高 生成的圖片質量比較好 但速度慢
		BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB ); 
		image.getGraphics().drawImage(img, 0, 0, w, h, null); // 繪製縮小後的圖
		FileOutputStream out = new FileOutputStream(destFile); // 輸出到檔案流
		// 可以正常實現bmp、png、gif轉jpg
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image); // JPEG編碼
		out.close();
	}
}