Question

I can set the attributes and create the dicom file, but I can not write the image to the dicom file?

Was it helpful?

Solution

I've tried it with an image I have and it works but I expect it won't work for RGB images. Something like this though

    BufferedImage jpg = ImageIO.read(new File("myjpg.jpg"));

    //Convert the image to a byte array 
    DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();
    short[] data = buff.getData();
    ByteBuffer byteBuf = ByteBuffer.allocate(2*data.length);
    int i = 0;
    while (data.length > i) {
        byteBuf.putShort(data[i]);
        i++;
    }

    //Copy a header 
    DicomInputStream dis = new DicomInputStream(new File("fileToCopyheaderFrom.dcm"));
    Attributes meta = dis.readFileMetaInformation();
    Attributes attribs = dis.readDataset(-1, Tag.PixelData);
    dis.close();

    //Change the rows and columns
    attribs.setInt(Tag.Rows, VR.US, jpg.getHeight());
    attribs.setInt(Tag.Columns, VR.US, jpg.getWidth());
    System.out.println(byteBuf.array().length);
    //Attributes attribs = new Attributes();

    //Write the file
    attribs.setBytes(Tag.PixelData, VR.OW, byteBuf.array());
    DicomOutputStream dcmo = new DicomOutputStream(new File("myDicom.dcm"));
    dcmo.writeFileMetaInformation(meta);
    attribs.writeTo(dcmo);
    dcmo.close();

Edit 1

I've assumed your image has an Unsigned short Data Buffer here.

DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();

To handle other data buffers you should check the type , cast acordingly and then convert to a byte array. For a byte Buffer it should be easy

DataBufferByte buff = (DataBufferByte) jpg.getData().getDataBuffer();

Then

buff.getData(numOfBank)

where numOfBank is 0 for you image

should return a byte array

OTHER TIPS

This Code is Convert JPG file to Dicom File

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.dcm4che2.data.DicomObject;
import org.dcm4che2.data.Tag;
import org.dcm4che2.data.VR;
import org.dcm4che2.io.DicomInputStream;
import org.dcm4che2.io.DicomOutputStream;

public class JPG_TO_DCM {
    public static void main(String a[]) throws FileNotFoundException, IOException
    {
    //  System.out.println("JPG2DCM_Convert.main()");
        String  patientID="123";   // Enter Patient ID
        String patientName="DCM IMAGE"; // Enter Pantient Name
        String studyID="123"; // Enter Study ID
        Jpg2Dcm jpg2Dcm = new Jpg2Dcm();  //
        File jpgFileOS = new File("D:\\AA.jpg");
        File dcmFileOS = new File("D:\\AA.dcm");

        try {
            jpg2Dcm.convert(jpgFileOS, dcmFileOS);   // Only Convert Jpg to Dicom without seting any dicom tag (without DicomHeader)
        } catch (IOException e) {
            e.printStackTrace();
        }


      try {
           anonymizeDicom(new File("D:\\AA.dcm"),new File("D:\\AA_FIANAL.dcm"),patientID,studyID,patientName);   // Set the DICOM Header
        } catch (Exception e) {
            e.printStackTrace();
        }



    }
    public static final  int[] TAGS = {
        Tag.PatientID,
        Tag.StudyID, 
        Tag.PatientName
    };
    public static void anonymizeDicom(File fileInput, File fileOutput,  String patientIDForUpdate, String studyIDForUpdate,String patientNameForUpdate) throws FileNotFoundException, IOException 
    {
        int patientID = 1048608;
        int studyID = 2097168;
        int patientName=1048592;
        try 
        {
            FileInputStream fis = new FileInputStream(fileInput);
            DicomInputStream dis = new DicomInputStream(fis);
            DicomObject obj = dis.readDicomObject();
            for (int tag : TAGS) 
            {
                if (tag == patientID) {
                    replaceTag(obj, tag, patientIDForUpdate);
                }
                if (tag == studyID) {
                    replaceTag(obj, tag, studyIDForUpdate);
                }

                if (tag == patientName) {
                    replaceTag(obj, tag, patientNameForUpdate);
                }

            }
            fis.close();
            dis.close();
            writeDicomFile(obj, fileOutput);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   @SuppressWarnings("unused")
   private static String[] getValue(DicomObject object, int[]  PATIENT_ADDITIONAL_TAGS)
   {
       String [] value = new String [PATIENT_ADDITIONAL_TAGS.length];
       int i =0;
       while (i<PATIENT_ADDITIONAL_TAGS.length)
        {
            for (int tag : PATIENT_ADDITIONAL_TAGS) 
            {
                      value[i]=object.getString(tag);
                      i++;
            }
        }
        return value;
   }

    public static void replaceTag(DicomObject dObj, int tag, String newValue) {
        if (tag != 0 && dObj.contains(tag)) {
            VR vr = dObj.vrOf(tag);
            try 
            {
                dObj.putString(tag, vr, newValue);
            } catch (Exception e) {
            //  System.err.println("Error replacing Tag: " + tag+ " with new value: " + newValue);
            }
        }
    }

    public static void writeDicomFile(DicomObject dObj, File f) {
        FileOutputStream fos;
        try 
        {
            fos = new FileOutputStream(f);
        }
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
            return;
        }
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        DicomOutputStream dos = new DicomOutputStream(bos);
        try {
            dos.writeDicomFile(dObj);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            try {
                dos.close();
            } catch (IOException ignore) {
            }
        }
    }
    static int count1 = 0;
    public static void getFile(String dirPath, String destdirPath,String patientID, String studyID,String patientNameForUpdate) throws FileNotFoundException,IOException 
    {
        File f = new File(dirPath);
        File[] files = f.listFiles();
        if (files != null)
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    getFile(file.getAbsolutePath(), destdirPath, patientID,studyID,patientNameForUpdate);
                } else {
                    count1++;
                    String name = file.getAbsolutePath();
                //  test7 test = new test7();
                    anonymizeDicom(new File(name), new File(destdirPath+ "/" + file.getName()), patientID, studyID,patientNameForUpdate);
                }
            }
    }

}

Jpg2Dcm.java File

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.dcm4che2.data.BasicDicomObject;
import org.dcm4che2.data.DicomObject;
import org.dcm4che2.data.Tag;
import org.dcm4che2.data.UID;
import org.dcm4che2.data.VR;
import org.dcm4che2.io.DicomOutputStream;
import org.dcm4che2.util.UIDUtils;


public class Jpg2Dcm {

    private static final String USAGE = 
        "jpg2dcm [Options] <jpgfile> <dcmfile>";

    private static final String DESCRIPTION = 
        "Encapsulate JPEG Image into DICOM Object.\nOptions:";

    private static final String EXAMPLE = 
        "--\nExample 1: Encapulate JPEG Image verbatim with default values " +
        "for mandatory DICOM attributes into DICOM Secondary Capture Image:" +
        "\n$ jpg2dcm image.jpg image.dcm" +
        "\n--\nExample 2: Encapulate JPEG Image without application segments " +
        "and additional DICOM attributes to mandatory defaults into DICOM " +
        "Image Object:" +
        "\n$ jpg2dcm --no-appn -c patattrs.cfg homer.jpg image.dcm" +
        "\n--\nExample 3: Encapulate MPEG2 Video with specified DICOM " +
        "attributes into DICOM Video Object:" +
        "\n$ jpg2dcm --mpeg -C mpg2dcm.cfg video.mpg video.dcm";

    private static final String LONG_OPT_CHARSET = "charset";

    private static final String OPT_CHARSET_DESC =
        "Specific Character Set code string, ISO_IR 100 by default";

    private static final String OPT_AUGMENT_CONFIG_DESC =
        "Specifies DICOM attributes included additional to mandatory defaults";

    private static final String OPT_REPLACE_CONFIG_DESC =
        "Specifies DICOM attributes included instead of mandatory defaults";

    private static final String LONG_OPT_TRANSFER_SYNTAX = "transfer-syntax";

    private static final String OPT_TRANSFER_SYNTAX_DESC =
        "Transfer Syntax; 1.2.840.10008.1.2.4.50 (JPEG Baseline) by default.";

    private static final String LONG_OPT_MPEG = "mpeg";

    private static final String OPT_MPEG_DESC =
        "Same as --transfer-syntax 1.2.840.10008.1.2.4.100 (MPEG2).";

    private static final String LONG_OPT_UID_PREFIX = "uid-prefix";

    private static final String OPT_UID_PREFIX_DESC =
        "Generate UIDs with given prefix, 1.2.40.0.13.1.<host-ip> by default.";

    private static final String LONG_OPT_NO_APPN = "no-appn";

    private static final String OPT_NO_APPN_DESC =
        "Exclude application segments APPn from JPEG stream; " +
        "encapsulate JPEG stream verbatim by default.";

    private static final String OPT_HELP_DESC =
        "Print this message";

    private static final String OPT_VERSION_DESC =
        "Print the version information and exit";

    private static int FF = 0xff;

    private static int SOF = 0xc0;

    private static int DHT = 0xc4;

    private static int DAC = 0xcc;

    private static int SOI = 0xd8;

    private static int SOS = 0xda;

    private static int APP = 0xe0;    

    private String charset = "ISO_IR 100";

    private String transferSyntax = UID.JPEGBaseline1;

    private byte[] buffer = new byte[8192];

    private int jpgHeaderLen;

    private int jpgLen;

    private boolean noAPPn = false;

    private Properties cfg = new Properties();

    public Jpg2Dcm() {
        try {
            cfg.load(Jpg2Dcm.class.getResourceAsStream("jpg2dcm.cfg"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public final void setCharset(String charset) {
        this.charset = charset;
    }

    private final void setTransferSyntax(String uid) {
        this.transferSyntax = uid;
    }


    private final void setNoAPPn(boolean noAPPn) {
        this.noAPPn = noAPPn;
    }

    private void loadConfiguration(File cfgFile, boolean augment)
            throws IOException {
        Properties tmp = augment ? new Properties(cfg) : new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(cfgFile));
        try {
            tmp.load(in);
        } finally {
            in.close();
        }
        cfg = tmp;
    }

    public void convert(File jpgFile, File dcmFile) throws IOException {
        jpgHeaderLen = 0;
        jpgLen = (int) jpgFile.length();
        DataInputStream jpgInput = new DataInputStream(
                new BufferedInputStream(new FileInputStream(jpgFile)));
        try {
            DicomObject attrs = new BasicDicomObject();
            attrs.putString(Tag.SpecificCharacterSet, VR.CS, charset);
            for (Enumeration en = cfg.propertyNames(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                int[] tagPath = Tag.toTagPath(key);                
                int last = tagPath.length-1;
                VR vr = attrs.vrOf(tagPath[last]);
                if (vr == VR.SQ) {
                    attrs.putSequence(tagPath);
                } else {
                    attrs.putString(tagPath, vr, cfg.getProperty(key));
                }
            }
            if (noAPPn || missingRowsColumnsSamplesPMI(attrs)) {
                readHeader(attrs, jpgInput);
            }
            ensureUS(attrs, Tag.BitsAllocated, 8);
            ensureUS(attrs, Tag.BitsStored, attrs.getInt(Tag.BitsAllocated));
            ensureUS(attrs, Tag.HighBit, attrs.getInt(Tag.BitsStored) - 1);
            ensureUS(attrs, Tag.PixelRepresentation, 0);
            ensureUID(attrs, Tag.StudyInstanceUID);
            ensureUID(attrs, Tag.SeriesInstanceUID);
            ensureUID(attrs, Tag.SOPInstanceUID);
            Date now = new Date();
            attrs.putDate(Tag.InstanceCreationDate, VR.DA, now);
            attrs.putDate(Tag.InstanceCreationTime, VR.TM, now);
            attrs.initFileMetaInformation(transferSyntax);
            FileOutputStream fos = new FileOutputStream(dcmFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            DicomOutputStream dos = new DicomOutputStream(bos);
            try {
                dos.writeDicomFile(attrs);
                dos.writeHeader(Tag.PixelData, VR.OB, -1);
                dos.writeHeader(Tag.Item, null, 0);
                dos.writeHeader(Tag.Item, null, (jpgLen+1)&~1);
                dos.write(buffer, 0, jpgHeaderLen);
                int r;
                while ((r = jpgInput.read(buffer)) > 0) {
                    dos.write(buffer, 0, r);
                }
                if ((jpgLen&1) != 0) {
                    dos.write(0);
                }
                dos.writeHeader(Tag.SequenceDelimitationItem, null, 0);
            } finally {
                dos.close();
            }
        } finally {
            jpgInput.close();            
        }
    }    

    private boolean missingRowsColumnsSamplesPMI(DicomObject attrs) {
        return !(attrs.containsValue(Tag.Rows) 
                && attrs.containsValue(Tag.Columns)
                && attrs.containsValue(Tag.SamplesPerPixel)
                && attrs.containsValue(Tag.PhotometricInterpretation)
                );
    }

    private void readHeader(DicomObject attrs, DataInputStream jpgInput)
            throws IOException {
        if (jpgInput.read() != FF || jpgInput.read() != SOI 
                || jpgInput.read() != FF) {
            throw new IOException(
                    "JPEG stream does not start with FF D8 FF");
        }
        int marker = jpgInput.read();
        int segmLen;
        boolean seenSOF = false;
        buffer[0] = (byte) FF;
        buffer[1] = (byte) SOI;
        buffer[2] = (byte) FF;
        buffer[3] = (byte) marker;
        jpgHeaderLen = 4;
        while (marker != SOS) {
            segmLen = jpgInput.readUnsignedShort();
            if (buffer.length < jpgHeaderLen + segmLen + 2) {
                growBuffer(jpgHeaderLen + segmLen + 2);
            }
            buffer[jpgHeaderLen++] = (byte) (segmLen >>> 8);
            buffer[jpgHeaderLen++] = (byte) segmLen;
            jpgInput.readFully(buffer, jpgHeaderLen, segmLen - 2);
            if ((marker & 0xf0) == SOF && marker != DHT && marker != DAC) {
                seenSOF = true;
                int p = buffer[jpgHeaderLen] & 0xff;
                int y = ((buffer[jpgHeaderLen+1] & 0xff) << 8)
                       | (buffer[jpgHeaderLen+2] & 0xff);
                int x = ((buffer[jpgHeaderLen+3] & 0xff) << 8)
                       | (buffer[jpgHeaderLen+4] & 0xff);
                int nf = buffer[jpgHeaderLen+5] & 0xff;
                attrs.putInt(Tag.SamplesPerPixel, VR.US, nf);
                if (nf == 3) {
                    attrs.putString(Tag.PhotometricInterpretation, VR.CS,
                            "YBR_FULL_422");
                    attrs.putInt(Tag.PlanarConfiguration, VR.US, 0);
                } else {
                    attrs.putString(Tag.PhotometricInterpretation, VR.CS,
                            "MONOCHROME2");
                }
                attrs.putInt(Tag.Rows, VR.US, y);
                attrs.putInt(Tag.Columns, VR.US, x);
                attrs.putInt(Tag.BitsAllocated, VR.US, p > 8 ? 16 : 8);
                attrs.putInt(Tag.BitsStored, VR.US, p);
                attrs.putInt(Tag.HighBit, VR.US, p-1);
                attrs.putInt(Tag.PixelRepresentation, VR.US, 0);
            }
            if (noAPPn & (marker & 0xf0) == APP) {
                jpgLen -= segmLen + 2;
                jpgHeaderLen -= 4;
            } else {
                jpgHeaderLen += segmLen - 2;
            }
            if (jpgInput.read() != FF) {
                throw new IOException("Missing SOS segment in JPEG stream");
            }
            marker = jpgInput.read();
            buffer[jpgHeaderLen++] = (byte) FF;
            buffer[jpgHeaderLen++] = (byte) marker;
        }
        if (!seenSOF) {
            throw new IOException("Missing SOF segment in JPEG stream");
        }
    }

    private void growBuffer(int minSize) {
        int newSize = buffer.length << 1;
        while (newSize < minSize) {
            newSize <<= 1;
        }
        byte[] tmp = new byte[newSize];
        System.arraycopy(buffer, 0, tmp, 0, jpgHeaderLen);
        buffer = tmp;
    }

    private void ensureUID(DicomObject attrs, int tag) {
        if (!attrs.containsValue(tag)) {
            attrs.putString(tag, VR.UI, UIDUtils.createUID());
        }        
    }

    private void ensureUS(DicomObject attrs, int tag, int val) {
        if (!attrs.containsValue(tag)) {
            attrs.putInt(tag, VR.US, val);
        }        
    }    
    public static void main(String[] args) {
        try {
            CommandLine cl = parse(args);
            Jpg2Dcm jpg2Dcm = new Jpg2Dcm();
            if (cl.hasOption(LONG_OPT_CHARSET)) {
                jpg2Dcm.setCharset(cl.getOptionValue(LONG_OPT_CHARSET));
            }
            if (cl.hasOption("c")) {
                jpg2Dcm.loadConfiguration(new File(cl.getOptionValue("c")), true);
            }
            if (cl.hasOption("C")) {
                jpg2Dcm.loadConfiguration(new File(cl.getOptionValue("C")), false);
            }
            if (cl.hasOption(LONG_OPT_UID_PREFIX)) {
                 UIDUtils.setRoot(cl.getOptionValue(LONG_OPT_UID_PREFIX));
            }
            if (cl.hasOption(LONG_OPT_MPEG)) {
                jpg2Dcm.setTransferSyntax(UID.MPEG2);
            }
            if (cl.hasOption(LONG_OPT_TRANSFER_SYNTAX)) {
                jpg2Dcm.setTransferSyntax(
                        cl.getOptionValue(LONG_OPT_TRANSFER_SYNTAX));
            }
            jpg2Dcm.setNoAPPn(cl.hasOption(LONG_OPT_NO_APPN));

            List argList = cl.getArgList();
            File jpgFile = new File("d:\\VIPUL.jpg");
            File dcmFile = new File("d:\\aa.dcm");
            long start = System.currentTimeMillis();
            jpg2Dcm.convert(jpgFile, dcmFile);
            long fin = System.currentTimeMillis();
            System.out.println("Encapsulated " + jpgFile + " to "  
                    + dcmFile + " in " + (fin - start) +  "ms.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static CommandLine parse(String[] args) {
        Options opts = new Options();

        OptionBuilder.withArgName("code");
        OptionBuilder.hasArg();
        OptionBuilder.withDescription(OPT_CHARSET_DESC);
        OptionBuilder.withLongOpt(LONG_OPT_CHARSET);
        opts.addOption(OptionBuilder.create());

        OptionBuilder.withArgName("file");
        OptionBuilder.hasArg();
        OptionBuilder.withDescription(OPT_AUGMENT_CONFIG_DESC);
        opts.addOption(OptionBuilder.create("c"));

        OptionBuilder.withArgName("file");
        OptionBuilder.hasArg();
        OptionBuilder.withDescription(OPT_REPLACE_CONFIG_DESC);
        opts.addOption(OptionBuilder.create("C"));

        OptionBuilder.withArgName("prefix");
        OptionBuilder.hasArg();
        OptionBuilder.withDescription(OPT_UID_PREFIX_DESC);
        OptionBuilder.withLongOpt(LONG_OPT_UID_PREFIX);
        opts.addOption(OptionBuilder.create());

        OptionBuilder.withArgName("uid");
        OptionBuilder.hasArg();
        OptionBuilder.withDescription(OPT_TRANSFER_SYNTAX_DESC);
        OptionBuilder.withLongOpt(LONG_OPT_TRANSFER_SYNTAX);
        opts.addOption(OptionBuilder.create());

        opts.addOption(null, LONG_OPT_MPEG, false, OPT_MPEG_DESC);

        opts.addOption(null, LONG_OPT_NO_APPN, false, OPT_NO_APPN_DESC);

        opts.addOption("h", "help", false, OPT_HELP_DESC);
        opts.addOption("V", "version", false, OPT_VERSION_DESC);

        CommandLine cl = null;
        try {
            cl = new PosixParser().parse(opts, args);
        } catch (ParseException e) {
            exit("jpg2dcm: " + e.getMessage());
            throw new RuntimeException("unreachable");
        }
        if (cl.hasOption('V')) {
            Package p = Jpg2Dcm.class.getPackage();
            System.out.println("jpg2dcm v" + p.getImplementationVersion());
            System.exit(0);
        }
        if (cl.hasOption('h') || cl.getArgList().size() != 2) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(USAGE, DESCRIPTION, opts, EXAMPLE);
            System.exit(0);
        }

        return cl;
    }

    private static void exit(String msg) {
        System.err.println(msg);
        System.err.println("Try 'jpg2dcm -h' for more information.");
        System.exit(1);
    }

}

Jpg2Dcm.java file use can also include below

org.dcm4che3.tool.dcm2jpg.Dcm2Jpg.java file

dcm4che-tool-dcm2jpg-3.3.7.jar

Also include this also

  1. commons-cli-1.2.jar
  2. dcm4che-core-2.0.26.jar
  3. log4j-boot.jar
  4. slf4j-api-1.5.0.jar
  5. slf4j-log4j12-1.5.0.jar

Solver:

public  jpgdcm(File file, File fileOutput) {
    try {   
            jpgLen = (int) file.length();  



    BufferedImage jpegImage = ImageIO.read(file);  
    /*
     * We open a try block and then read our Jpeg into a BufferedImage through ImageIO.read() method. If this results in an invalid image so we may throw a new exception. 
     * Else, we’ve got a valid image and therefore valuable information about it. 
     * The BufferedImage class has a lot of useful methods for retrieving image data, then let’s save the number of color components (samples per pixel) of our image, the bits
     *  per pixel and the bits allocated:

     */

      if (jpegImage == null) throw new Exception("Invalid file."); 
     /* We open a try block and then read our Jpeg into a BufferedImage through ImageIO.read() method. 
      * If this results in an invalid image so we may throw a new exception. 
      * Else, we’ve got a valid image and therefore valuable information about it.
      * The BufferedImage class has a lot of useful methods for retrieving image data, 
      * then let’s save the number of color components (samples per pixel) of our image, 
      * the bits per pixel and the bits allocated: */

         int colorComponents = jpegImage.getColorModel().getNumColorComponents();  
         int bitsPerPixel = jpegImage.getColorModel().getPixelSize();  
         int bitsAllocated = (bitsPerPixel / colorComponents);  
         int samplesPerPixel = colorComponents; 
     /*It’s time to start building our Dicom dataset:*/

        Attributes dicom = new Attributes();  
         dicom.setString(Tag.SpecificCharacterSet, VR.CS, "ISO_IR 100");  
         dicom.setString(Tag.PhotometricInterpretation, VR.CS, samplesPerPixel == 3 ? "YBR_FULL_422" : "MONOCHROME2"); 

     /*The first line creates a new basic Dicom object defined by dcm4che2 toolkit. 
      *The next one puts header information for Specific Character Set: ISO_IR 100 – it’s the same for ISO-8859-1 – the code for Latin alphabet.
      *Finally, the last line puts header information for photometric interpretation (read with or without colors). 
      *So if our image has samples per pixel equals to 3, it has colors (YBR_FULL_422), else it’s a grayscale image (MONOCHROME2).
      The following lines add integer values to our Dicom header. Note that all of them comes from BufferedImage methods. 
      These values are mandatory when encapsulating. For more information you can check Part 3.5 of Dicom Standard. */

         dicom.setInt(Tag.SamplesPerPixel, VR.US, samplesPerPixel);           
         dicom.setInt(Tag.Rows, VR.US, jpegImage.getHeight());  
         dicom.setInt(Tag.Columns, VR.US, jpegImage.getWidth());  
         dicom.setInt(Tag.BitsAllocated, VR.US, bitsAllocated);  
         dicom.setInt(Tag.BitsStored, VR.US, bitsAllocated);  
         dicom.setInt(Tag.HighBit, VR.US, bitsAllocated-1);  
         dicom.setInt(Tag.PixelRepresentation, VR.US, 0); 
         /*Also, our Dicom header needs information about date and time of creation:*/
         dicom.setDate(Tag.InstanceCreationDate, VR.DA, new Date());  
         dicom.setDate(Tag.InstanceCreationTime, VR.TM, new Date());  
         /* Every Dicom file has a unique identifier. 
          * Here we’re generating study, series and Sop instances UIDs. 
          * You may want to modify these values, but you should to care about their uniqueness. 
          */
         dicom.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());  
         dicom.setString(Tag.SeriesInstanceUID, VR.UI, UIDUtils.createUID());  
         dicom.setString(Tag.SOPInstanceUID, VR.UI, UIDUtils.createUID());  
         dicom.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());
             dicom.setString(Tag.SeriesInstanceUID, VR.UI, UIDUtils.createUID());   
             dicom.setString(Tag.SOPInstanceUID, VR.UI, UIDUtils.createUID());
         /*Our Dicom header is almost done. 
          * The following command initiates Dicom metafile information considering JPEGBaseline1 as transfer syntax.
          * This means this file has Jpeg data encapsulated instead common medical image pixel data. 
          * The most common Jpeg files use a subset of the Jpeg standard called baseline Jpeg. 
          * A baseline Jpeg file contains a single image compressed with the baseline discrete cosine transformation (DCT) and Huffman encoding.
          */
        // dicom.initFileMetaInformation(UID.JPEGBaseline1);  
         /*After initiate the header we can open an output stream for saving our Dicom dataset as follows:*/

              Attributes fmi = new Attributes();

                 fmi.setString(Tag.ImplementationVersionName, VR.SH, "DCM4CHE3"); 
                  fmi.setString(Tag.ImplementationClassUID, VR.UI, UIDUtils.createUID());
                  fmi.setString(Tag.TransferSyntaxUID, VR.UI, transferSyntax);
                  fmi.setString(Tag.MediaStorageSOPClassUID, VR.UI, transferSyntax);
                  fmi.setString(Tag.MediaStorageSOPInstanceUID, VR.UI,UIDUtils.createUID());
                  fmi.setString(Tag.FileMetaInformationVersion, VR.OB, "1"); 
                  fmi.setInt(Tag.FileMetaInformationGroupLength, VR.UL, dicom.size()+fmi.size());



         DicomOutputStream dos = new DicomOutputStream(fileOutput);  
         dos.writeDataset(fmi, dicom);  
         dos.writeHeader(Tag.PixelData, VR.OB, -1); 

         /*
          * The Item Tag (FFFE,E000) is followed by a 4 byte item length field encoding the explicit number of bytes of the item.
          * The first item in the sequence of items before the encoded pixel data stream shall be a basic item with length equals to zero:
          */
         dos.writeHeader(Tag.Item, null, 0);
         /*The next Item then keeps the length of our Jpeg file. */
         /* 
         According to Gunter from dcm4che team we have to take care that  
         the pixel data fragment length containing the JPEG stream has  
         an even length. 
         */  

         dos.writeHeader(Tag.Item, null, (jpgLen+1)&~1);  
         /* Now all we have to do is to fill this item with bytes taken from our Jpeg file*/
         FileInputStream fis = new FileInputStream(file);  
         BufferedInputStream bis = new BufferedInputStream(fis);  
         DataInputStream dis = new DataInputStream(bis);  

         byte[] buffer = new byte[65536];         
         int b;  
         while ((b = dis.read(buffer)) > 0) {  
             dos.write(buffer, 0, b);  
         }  
         /*Finally, the Dicom Standard tells that we have to put a last Tag: 
          * a Sequence Delimiter Item (FFFE,E0DD) with length equals to zero.*/


         if ((jpgLen&1) != 0) dos.write(0);   
           dos.writeHeader(Tag.SequenceDelimitationItem, null, 0);  
           dos.close();  


    } catch (Exception e) {

    e.printStackTrace();
            }

   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top