java dcm文件转图片
1.情景展示
如何将dicom文件转成图片?
2.具体分析
所需jar包
<!--dcm文件转图片-->
<!-- https://mvnrepository.com/artifact/org.dcm4che/dcm4che-core -->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-core</artifactId>
<version>5.27.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dcm4che/dcm4che-imageio -->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio</artifactId>
<version>5.27.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dcm4che/dcm4che-image -->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-image</artifactId>
<version>5.27.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dcm4che.tool/dcm4che-tool-dcm2jpg -->
<dependency>
<groupId>org.dcm4che.tool</groupId>
<artifactId>dcm4che-tool-dcm2jpg</artifactId>
<version>5.23.3</version>
</dependency>
3.解决方案
代码实现
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.dcm4che3.util.SafeClose;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
private static ImageReader imageReader = ImageIO.getImageReadersByFormatName("DICOM").next();
/**
* 将DCM文件转成图片格式
* @param src dcm文件
* @param target 图片文件
*/
public static void toImage(File src, File target) {
ImageInputStream iis = null;
BufferedImage bi;
OutputStream out = null;
try{
iis = ImageIO.createImageInputStream(src);
imageReader.setInput(iis, false);
bi = imageReader.read(0);
// 获取图片后缀名
String formatName = FileUtils.getFileFormat(target);
//out = new BufferedOutputStream(new FileOutputStream(dest));
//JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(out); //这里也可以使用流将图像导出到web应用,用来搭建web版的PACS等。
//enc.encode(bi);
ImageIO.write(bi, formatName, target);
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
} finally{// 关闭流
SafeClose.close(iis);
SafeClose.close(out);
}
}
/**
* dcm文件转图片
* @param dcmPath dcm文件所在全路径
* C:\Users\Marydon\Desktop\16641003.dcm
* @param imgPath 图片文件所在全路径
* C:\Users\Marydon\Desktop\666.png
*/
public static void toImage(String dcmPath, String imgPath) {
toImage(new File(dcmPath), new File(imgPath));
}
测试
public static void main(String[] args) {
toImage("C:\\Users\\Marydon\\Desktop\\16641003.dcm", "C:\\Users\\Marydon\\Desktop\\777.jpg");
}
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/16966968.html