导入依赖
因为在maven仓库中可以找到相关的依赖,但是导入会失败,所以这里采用了本地包导入的方式
首先下载依赖包这里使用了百度网盘:
链接:https://pan.baidu.com/s/1MzPyA3xORbSZ3KLlcNRf3A
提取码:b5gx
大家可以自行使用:
下载好后在项目的resource
目录下创建一个lib文件夹将依赖包放进去
然后选中所有依赖包 右键 -> Add as Library
加载到项目中
然后在pom.xml文件中导入相关的依赖
<!-- dcm转png 依赖 -->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-tool-dcm2jpg</artifactId>
<version>5.22.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/dcm4che-tool-dcm2jpg-5.22.5.jar</systemPath>
</dependency>
<!--dcm4che依赖-->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-image</artifactId>
<version>5.22.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/dcm4che-image-5.22.5.jar</systemPath>
</dependency>
<!--dcm4che依赖-->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio</artifactId>
<version>5.22.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/dcm4che-imageio-5.22.5.jar</systemPath>
</dependency>
<!--dcm4che依赖-->
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-core</artifactId>
<version>5.22.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/dcm4che-core-5.22.5.jar</systemPath>
</dependency>
编写DCM文件转jpg文件的工具类
然后编写dcm文件转jpg文件的工具类
package cn.stylefeng.guns.modular.utils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;
import org.dcm4che3.util.SafeClose;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* DCM文件转jpg格式工具类
*
* @author An
* @date 2022/7/14 10:40
*/
@Setter
@Slf4j
@Configuration
public class DcmToJpgUtil {
private static final Logger logger = LoggerFactory.getLogger(DcmToJpgUtil.class);
private final ImageReader imageReader = ImageIO.getImageReadersByFormatName("DICOM").next();
private float windowCenter;
private float windowWidth;
private boolean autoWindowing = true;
private int windowIndex;
private int voiLUTIndex;
private boolean preferWindow = true;
private Attributes prState;
private int overlayActivationMask = 0xffff;
private int overlayGrayscaleValue = 0xffff;
private int frame = 1;
/**
* 将 DCM格式的图片转化为 jpg格式
* @param src dcm文件
* @param dest jpg文件
* @throws IOException
*/
public void convert(File src, File dest) throws IOException {
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
ImageReader reader = iter.next();
ImageInputStream iis = ImageIO.createImageInputStream(src);
BufferedImage bi;
OutputStream out = null;
try{
reader.setInput(iis, false);
bi = readImage(iis);
if (bi == null) {
logger.error("\nError: " + src + " - couldn't read!");
return;
}
String formatName = dest.getName().substring(dest.getName().lastIndexOf(".") + 1);
//out = new BufferedOutputStream(new FileOutputStream(dest));
//JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(out); //这里也可以使用流将图像导出到web应用,用来搭建web版的PACS等。
ImageIO.write(bi, /*"GIF"*/ formatName /* format desired */ , dest /* target */ );
//enc.encode(bi);
}finally{
SafeClose.close(iis);
SafeClose.close(out);
}
}
private ImageReadParam readParam(){
DicomImageReadParam param = (DicomImageReadParam) imageReader.getDefaultReadParam();
param.setWindowCenter(windowCenter);
param.setWindowWidth(windowWidth);
param.setAutoWindowing(autoWindowing);
param.setWindowIndex(windowIndex);
param.setVOILUTIndex(voiLUTIndex);
param.setPreferWindow(preferWindow);
param.setPresentationState(prState);
param.setOverlayActivationMask(overlayActivationMask);
param.setOverlayGrayscaleValue(overlayGrayscaleValue);
return param;
}
private BufferedImage readImage(ImageInputStream iis) throws IOException{
imageReader.setInput(iis);
return imageReader.read(frame -1, readParam());
}
}
调用编写工具类
最后调用工具类
/**
* 遍历所有的文件 并将文件转化成jpg文件
* @param path 需要遍历的文件路径
* @param DCMList 存放jpg文件的数组
* @return
*/
public List<File> search(String path, List<File> DCMList) {
List<File> DCMFileList = DCMList;
long start = 0;
File dir = new File(path);
File[] subFiles = dir.listFiles(); //获取e盘下所有的文件或文件夹对象
if (null != subFiles) {
for (File subFile : subFiles) {
if (subFile.isDirectory()) { //文件夹则递归寻找,后缀为dcm文件则输出名字
search(subFile.getAbsolutePath(), DCMFileList);
} else if (subFile.isFile() && subFile.getName().endsWith(".dcm")) {
start = System.currentTimeMillis();
try {
File jpg = new File(jpgPath + subFile.getName() + ".jpg");
dcmToJpgUtil.convert(subFile, jpg);
DCMFileList.add(jpg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
long end = System.currentTimeMillis();
logger.info(">>>>>>>>>> 转化JPG完成,用时:" + (end - start) + " ms");
return DCMFileList;
}
然后就可以实现DCM装jpg文件了相对比较简单的