参考1 JAVA代码实现DICOM文件转换JPG
package com.example;import java.awt.image.BufferedImage;
import java.io.File;import javax.imageio.ImageIO;import ij.plugin.DICOM;/*** dicom文件java解析,生成图片* 不过这里不能解析压缩的dicom文件*/
public class ImageDemo {public static void main(String args[]) {
// create("test1.dcm"); //在本地目录生成test1.dcm.jpg图片文件create2("D:\\dicom\\test3.dcm"); //在电脑dicom文件夹下生成test1.dcm.jpg图片文件}/*** 根据dicom文件生成jpg图片* <p/>* 这里输入的是image文件夹的dicom文件名字,* 运行即可得到一个jpg图片,显示的是dicom里面的图形*/private static void create(String fileName) {try {String projectPath = System.getProperty("user.dir");//Check class DICOMDICOM dicom = new DICOM();String imagePath = projectPath + "\\image\\" + fileName;dicom.run(imagePath);BufferedImage bi = (BufferedImage) dicom.getImage();int width = bi.getWidth();int height = dicom.getHeight();System.out.println("width: " + width + "\n" + "height: " + height);imagePath = projectPath + "\\image\\" + fileName + ".jpg";ImageIO.write(bi, "jpg", new File(imagePath));System.out.println("Hehe,Game over!!!");} catch (Exception e) {System.out.println("错误" + e.getMessage());}}/*** 输入一个dicom文件的绝对路径和名字* 获取一个jpg文件*/private static void create2(String filePath) {try {DICOM dicom = new DICOM();dicom.run(filePath);BufferedImage bi = (BufferedImage) dicom.getImage();int width = bi.getWidth();int height = dicom.getHeight();System.out.println("width: " + width + "\n" + "height: " + height);String imagePath = filePath + ".jpg";ImageIO.write(bi, "jpg", new File(imagePath));System.out.println("Hehe,Game over!!!");} catch (Exception e) {System.out.println("错误" + e.getMessage());}}
}
参考2 使用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基本随便百度搜索就能解决了。
只需要一个主文件即可:
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/” 是转换后文件所在目录。
参考3 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);//上传到ossOssUtil.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());}
}
参考4 java代码使用ImageJ解析dicom文件成图片
Dicom全称是医学数字图像与通讯,这里讲java解析diocm格式文件变成jpg示例。
这里的代码只能解析普通的dicom文件成jpg图片,对于压缩的dicom文件是没有办法解析的!
ImageJ解析代码很简单,但是要导入ImageJ的jar包。
从第一个图片可以看到lib中导入了ij.jar,这就是ImageJ的jar包,下面那个zip是source文件,这样就可以看到ImageJ里面的源码。
下面是调用ImageJ的代码:
import ij.plugin.DICOM;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;/*** dicom文件java解析,生成图片* 不过这里不能解析压缩的dicom文件*/
public class ImageDemo {public static void main(String args[]) {
// create("test1.dcm"); //在本地目录生成test1.dcm.jpg图片文件create2("D:\\dicom\\test3.dcm"); //在电脑dicom文件夹下生成test1.dcm.jpg图片文件}/*** 根据dicom文件生成jpg图片* <p/>* 这里输入的是image文件夹的dicom文件名字,* 运行即可得到一个jpg图片,显示的是dicom里面的图形*/private static void create(String fileName) {try {String projectPath = System.getProperty("user.dir");//Check class DICOMDICOM dicom = new DICOM();String imagePath = projectPath + "\\image\\" + fileName;dicom.run(imagePath);BufferedImage bi = (BufferedImage) dicom.getImage();int width = bi.getWidth();int height = dicom.getHeight();System.out.println("width: " + width + "\n" + "height: " + height);imagePath = projectPath + "\\image\\" + fileName + ".jpg";ImageIO.write(bi, "jpg", new File(imagePath));System.out.println("Hehe,Game over!!!");} catch (Exception e) {System.out.println("错误" + e.getMessage());}}/*** 输入一个dicom文件的绝对路径和名字* 获取一个jpg文件*/private static void create2(String filePath) {try {DICOM dicom = new DICOM();dicom.run(filePath);BufferedImage bi = (BufferedImage) dicom.getImage();int width = bi.getWidth();int height = dicom.getHeight();System.out.println("width: " + width + "\n" + "height: " + height);String imagePath = filePath + ".jpg";ImageIO.write(bi, "jpg", new File(imagePath));System.out.println("Hehe,Game over!!!");} catch (Exception e) {System.out.println("错误" + e.getMessage());}}
}
参考5 lj.jar
#ImageJ解析dicom文件成jpg图片 Dicom全称是医学数字图像与通讯,这里讲java解析diocm格式文件变成jpg示例。 这里的代码只能解析普通的dicom文件成jpg图片,对于压缩的dicom文件是没有办法解析的!
ImageJ解析代码很简单,但是要导入ImageJ的jar包。 从第一个图片可以看到lib中导入了ij.jar,这就是ImageJ的jar包,下面那个zip是source文件,这样就可以看到ImageJ里面的源码。
下面是调用ImageJ的代码:
import ij.plugin.DICOM;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;/*** dicom文件java解析,生成图片* 不过这里不能解析压缩的dicom文件*/
public class ImageDemo {public static void main(String args[]) {
// create("test1.dcm"); //在本地目录生成test1.dcm.jpg图片文件create2("D:\\dicom\\test3.dcm"); //在电脑dicom文件夹下生成test1.dcm.jpg图片文件}/*** 根据dicom文件生成jpg图片* <p/>* 这里输入的是image文件夹的dicom文件名字,* 运行即可得到一个jpg图片,显示的是dicom里面的图形*/private static void create(String fileName) {try {String projectPath = System.getProperty("user.dir");//Check class DICOMDICOM dicom = new DICOM();String imagePath = projectPath + "\\image\\" + fileName;dicom.run(imagePath);BufferedImage bi = (BufferedImage) dicom.getImage();int width = bi.getWidth();int height = dicom.getHeight();System.out.println("width: " + width + "\n" + "height: " + height);imagePath = projectPath + "\\image\\" + fileName + ".jpg";ImageIO.write(bi, "jpg", new File(imagePath));System.out.println("Hehe,Game over!!!");} catch (Exception e) {System.out.println("错误" + e.getMessage());}}/*** 输入一个dicom文件的绝对路径和名字* 获取一个jpg文件*/private static void create2(String filePath) {try {DICOM dicom = new DICOM();dicom.run(filePath);BufferedImage bi = (BufferedImage) dicom.getImage();int width = bi.getWidth();int height = dicom.getHeight();System.out.println("width: " + width + "\n" + "height: " + height);String imagePath = filePath + ".jpg";ImageIO.write(bi, "jpg", new File(imagePath));System.out.println("Hehe,Game over!!!");} catch (Exception e) {System.out.println("错误" + e.getMessage());}}
}