喵星之旅-狂奔的兔子-使用java实现dcm文件转jpg

由于相关的jar基本直接搞不到,需要自己编译jar。这里提供直接带有jar的项目,免去编译问题,很多时候编译失败。

jar一共需要4个:

dcm4che-core-5.20.0.jar

dcm4che-imageio-5.20.0.jar

dcm4che-image-5.20.0.jar

slf4j-api-1.7.30.jar

如果能拿到这几个jar基本随便百度搜索就能解决了。

 

 

代码地址:

 

svn://47.105.188.20/kitty/2、code/bunny_dcmtool  用户名密码: reader reader

 

 

只需要一个主文件即可:

 

package club.kittybunny.tool.dcmfile;


import org.dcm4che3.data.Attributes;
import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;


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.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;


public class Dcm2Jpg2 {
    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 args
 * @throws Exception
 */
    public static void main(String[] args) throws Exception {
        //源文件加目录全路径,结尾用/结束,只处理里面dcm格式文件 ,例如"/home/bunny/桌面/src/"
        String src = "/home/bunny/Downloads/qq-files/1023354682/file_recv/3000566.000000-03192/";
        //目标路径全路径,将源文件加内所有dcm文件转换为同名jpg文件,使用/结束,;例如 "/home/bunny/桌面/w/dcm/"
        String out = "/home/bunny/桌面/w/dcm/";
        if (args.length >= 2) {
            src = args[0];
            out = args[1];
            
        }
        File f = new File(src);
        //读取到所有文件名
        String[] arr = f.list();
        for (int i = 0; i < arr.length; i++) {
            //筛选符合条件的文件处理
            if (arr[i].endsWith(".dcm")) {
                InputStream in = new FileInputStream(src + arr[i]);

                ByteArrayOutputStream byteArrayOutputStream = new Dcm2Jpg2().convert(in, null);
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = new FileOutputStream(out + arr[i].substring(0, arr[i].lastIndexOf("."))+".jpg");
                    fileOutputStream.write(byteArrayOutputStream.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        
    }

    /**
     * 
     * 实现单文件的dcm转jpg
     * @param src  dcm文件流
     * @param name oss的getFileName
     * @throws Exception
     */
    public ByteArrayOutputStream convert(InputStream src, String name) throws Exception {
        Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
        ImageReader reader = iter.next();
        try (ImageInputStream iis = ImageIO.createImageInputStream(src)) {
            reader.setInput(iis, false);
            BufferedImage bi = readImage(iis);
            if (bi == null) {
                throw new Exception("无法读取到图片");
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", os);

            return os;
            
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

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

 

附带可执行jar的说明文件:

先将需要转换的dcm文件统一放到一个文件夹内,然后创建转换后文件的文件夹,文件夹全路径不能有空格。
执行命令例如:
java -jar dcm2jpg.jar /home/bunny/Downloads/qq-files/1023354682/file_recv/3000566.000000-03192/ /home/bunny/桌面/w/new/

其中第一个参数是源文件所在目录。
第二个参数是转换后文件艘在目录。
所有目录需要“/”结尾。

上面示例,命令中
"/home/bunny/Downloads/qq-files/1023354682/file_recv/3000566.000000-03192/"是原文件所在目录;
"/home/bunny/桌面/w/new/" 是转换后文件所在目录。

 

posted @ 2020-08-03 15:30  喵星兔  阅读(1017)  评论(0编辑  收藏  举报