关于DICOM转JPG

xiaoxiao2021-02-28  29

关于一下代码几点说明:

1.使用以下代码能够将标准的DICOM文件解析为JPG格式的文件;

2.DICOM文件内没有Image时,程序会将不能处理的DICOM文件转出;

3.在ImageReadParam强转为DicomImageReadParam时有时会有问题,希望有经验的大神帮忙解决

4.以下代码部分来源于网络;地址:https://blog.csdn.net/ShallDid/article/details/77446462

5.如果有什么问题,希望各位大神指正,再次感谢;

A:工具类(将不能转换的DICOM文件复制到缓存文件夹)

package Other.Util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;import java.util.ArrayList;import java.util.List;/*** @author WL* @version 1.0* @CreatTime 2018年6月20日 上午12:49:50*/public class MyUtil { /** * 将文件从一个位置复制到另一个位置 * (使用FileChannel复制) * 2018-06-20 GX */ @SuppressWarnings("resource") public static void CopyFileUsingFileChannels(File source, File dest) throws IOException {            FileChannel inputChannel = null;            FileChannel outputChannel = null;         try{         inputChannel = new FileInputStream(source).getChannel();         outputChannel = new FileOutputStream(dest).getChannel();         outputChannel.transferFrom(inputChannel, 0, inputChannel.size());     }finally{         inputChannel.close();         outputChannel.close();     }

}

}

B:核心类DICOM转JPG

package Other.Util;import java.awt.image.BufferedImage;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.imageio.ImageIO;import javax.imageio.ImageReadParam;import javax.imageio.ImageReader;import javax.imageio.stream.ImageInputStream;import org.dcm4che3.data.Attributes;import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;import org.dcm4che3.util.SafeClose;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.parser.Feature;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class DcmTOJpg {    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;       public int getFrame() {        return frame;    }    public void setFrame(int frame) {        this.frame = frame;    }    public void setWindowCenter(float windowCenter) {        this.windowCenter = windowCenter;    }    public void setWindowWidth(float windowWidth) {        this.windowWidth = windowWidth;    }    public void setAutoWindowing(boolean autoWindowing) {        this.autoWindowing = autoWindowing;    }    public void setWindowIndex(int windowIndex) {        this.windowIndex = windowIndex;    }    public void setVoiLUTIndex(int voiLUTIndex) {        this.voiLUTIndex = voiLUTIndex;    }    public void setPreferWindow(boolean preferWindow) {        this.preferWindow = preferWindow;    }    public void setPrState(Attributes prState) {        this.prState = prState;    }    public void setOverlayActivationMask(int overlayActivationMask) {        this.overlayActivationMask = overlayActivationMask;    }    public void setOverlayGrayscaleValue(int overlayGrayscaleValue) {        this.overlayGrayscaleValue = overlayGrayscaleValue;    }    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) {                System.out.println("\nError: " + src + " - couldn't read!");                return;            }            out = new BufferedOutputStream(new FileOutputStream(dest));            //这里也可以使用流将图像导出到web应用,用来搭建web版的PACS等            JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(out);              enc.encode(bi);         }finally{            SafeClose.close(iis);            SafeClose.close(out);        }    }    private ImageReadParam readParam(){    //获取Object对象    Object Temp = imageReader.getDefaultReadParam();    //实例化子类对象    DicomImageReadParam param = new DicomImageReadParam();    //判定Object对象是否为实例化对象    if(Temp instanceof DicomImageReadParam) {    param = (DicomImageReadParam)Temp;    System.out.println(" ---> 判断Object对象是否为实例化对象DicomImageReadParam: " + Temp.equals(param));    }else {    param = (DicomImageReadParam)Temp;    System.out.println(" ---> 确定Object对象不是实例化对象DicomImageReadParam: 通过强转实现!" + param.getClass());    //    String STR = JSON.toJSON(Temp).toString();//    param = JSON.parseObject(STR, param.getClass());//    System.out.println(" ---> 确定Object对象不是实例化对象DicomImageReadParam: 通过JSON进行转换!" + param.getClass());        }//      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());    }

}

C:具体调用

package Test_DCM2IMAGE;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date;import Other.Util.DcmTOJpg;import Other.Util.MyUtil;/*** @author WL* @version 1.0* @CreatTime 2018年6月21日 下午12:39:02*/public class NoStandardDCM2IMAGE { //Dicom源文件地址 public static String DCM_PATH = "E:/DESKTOP/PACS/TEST_NoStandardDcm2Image/Source_File"; //不能处理的Dicom文件临时存储位置 publicstatic String DCM_TEMP_PATH = "E:/DESKTOP/PACS/TEST_NoStandardDcm2Image/Temp_Folder/"; //DCM转换成Image后存储位置 public static String JPG_PATH = "E:/DESKTOP/PACS/TEST_NoStandardDcm2Image/Produce_Image"; /** * DCM2IMAGE */ private static void DCM2IMAGE(String brID, String pacsID, String PEID) { //参数 String brIDA = brID; String pacsIDA = pacsID; String PEIDA = PEID; //源文件路径 File src = null; //Source_File源文件名 String File_Name = null; //Image的fileName String fileName = null; try { File DicomFile = new File(DCM_PATH); File [] File_List = DicomFile.listFiles(); if (File_List.length != 0) { for (int i = 0; i < File_List.length; i++) { File_Name = File_List[i].getName(); System.out.println("NAME: " + File_Name + " Convert to Image" + " ---> STARTED "); //dcm文件的具体位置 src = new File(DCM_PATH +"/"+ File_Name); //PACSID+指定字符生成唯一文件夹 String Folder_Name = "IMAGE_PACS_" + pacsIDA; //判断存放Image文件夹是否存在 File Dest_Folder = new File(JPG_PATH +"/"+ Folder_Name); if(!Dest_Folder.exists()) { Dest_Folder.mkdirs(); fileName = String.valueOf(1); }else { File [] Image_List = Dest_Folder.listFiles();         fileName = String.valueOf((Image_List.length) + 1); } //生成的JPEG图像位置  File dest = new File(Dest_Folder +"/"+ fileName + ".jpg");           DcmTOJpg dcm2jpg = new DcmTOJpg(); dcm2jpg.convert(src, dest); src.delete(); System.out.println("NAME: " + File_Name + " Convert to Image" + " ---> SUCCESS " + "\n"); } } } catch (Exception e) {// e.printStackTrace(); String Message = e.getMessage(); String STRA = "ImageReadParam"; String STRB = "DicomImageReadParam"; String STRC = "imageIndex: 0"; if(Message.indexOf(STRA) != -1 || Message.indexOf(STRB) != -1) { System.out.println("ImageReadParam CAST TO DicomImageReadParam FAIL"); //递归调用 DCM2IMAGE(brIDA, pacsIDA, PEIDA); }else if(Message.indexOf(STRC) != -1) { System.out.println(" ---> " + File_Name + " - <NO DICOM IMAGE FOUND>"); String Temp_Folder_Name = "DICOM_TEMP_" + pacsIDA; //判断指定文件夹是否存在 File Temp_Folder = new File(DCM_TEMP_PATH +"/"+ Temp_Folder_Name); if(!Temp_Folder.exists()) { Temp_Folder.mkdirs(); } //不能处理的Dicom文件临时存储位置 File Temp = new File(Temp_Folder +"/"+ File_Name);  try { MyUtil.CopyFileUsingFileChannels(src, Temp); System.out.println("COPY: " + File_Name + " - TO [" + Temp_Folder_Name + "] <COMPLETED>"); src.delete(); System.out.println("DELETE: " + File_Name + " - FROM SRC <COMPLETED>" + "\n"); } catch (IOException e1) { e1.printStackTrace(); } //递归调用 DCM2IMAGE(brIDA, pacsIDA, PEIDA); } } } /** * 测试使用方法 * @param args */ public static void main(String[] args) { long startTime = System.currentTimeMillis(); String brIDB = "A0247896"; String pacsIDB = "MR18-16054"; // String brIDB = "A0544883";// String pacsIDB = "CT18-18435"; // String brIDB = "B0243450";// String pacsIDB = "CT18-18472"; // String brIDB = "A0544883";// String pacsIDB = "CT18-18666"; String PEIDB = null; //获取DICOM文件// GET_DICOM.getPacsFromDevice(brIDB, pacsIDB, PEIDB); //将DICOM文件转成IMAGE DCM2IMAGE(brIDB, pacsIDB, PEIDB);// GET_FILETYPE(); long endTime = System.currentTimeMillis(); System.out.println(" ---> DICOM convert to Image OR Copy to TEMP Completed! ---> 程序耗时:"+ (endTime - startTime) + "ms"); }

}

D:控制台打印效果

转载请注明原文地址: https://www.6miu.com/read-2150376.html

最新回复(0)