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();
	}
}



你可能感兴趣的:(java)