java解析dcm文件到jpg

1,下载源码编译

下载dcm3che源码

mvn install

2,gradle/maven 引用

compile ('org.dcm4che.tool:dcm4che-tool-dcm2jpg:5.20.0'){
        exclude group: 'org.slf4j',module: 'slf4j-log4j12'
    }

3,编写工具类 


import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;


/**
 * dcm转jpg工具
 *
 * @author zhy
 */
@Setter
@Slf4j
public class Dcm2jpgUtil {

    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获取图片并上传oss
     *
     * @param src  dcm文件流
     * @param name oss的getFileName
     */
    public void convert(InputStream src, String name) {
        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 自定义异常("无法读取到图片");
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", os);
            //上传到oss
            OssUtil.uploadByInputStream(new ByteArrayInputStream(os.toByteArray()), name);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

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

 

源码中的opencv依赖

 可以在github中下载

windows|linux

发布了139 篇原创文章 · 获赞 114 · 访问量 24万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览