Home  >  Article  >  Java  >  Example analysis of Java parsing DICOM diagram to obtain hexadecimal data

Example analysis of Java parsing DICOM diagram to obtain hexadecimal data

黄舟
黄舟Original
2017-10-18 09:51:161727browse

DICOM stands for Medical Digital Imaging and Communication. It is the international standard for medical images and related information (ISO 12052). The following article mainly introduces you to the relevant information on how to obtain hexadecimal data by using Java to parse DICOM diagrams. The article introduces it in detail through sample code. Friends in need can refer to it.

Preface

In a recent project, JAVA was needed to parse DICOM images. DICOM is widely used in radiological medicine, cardiovascular imaging and Radiation diagnosis and treatment diagnostic equipment (X-ray, CT, MRI, ultrasound, etc.), and has been increasingly widely used in other medical fields such as ophthalmology and dentistry. Some problems encountered during implementation will be recorded below.

First find a *.dcm file. Open it with the editor and you will see the following interface. The editor I use is UltraEdit

The red letters mark the bytecode marks. The first 8 lines of code are the header information of the file and are generally useless. The four hexadecimal numbers "44,49,43,4D" starting on the ninth line are important. The ASCll code explanation is DICM. Indicates that this is a DICOM file. If these four hexadecimal numbers are lost or damaged, the DICOM image cannot be opened.

The following uses java to read these hexadecimal numbers


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

public class My_DICOM {

 static FileInputStream input;
 static byte[] b;
 public static void main(String[] args) {
  try {
   File file = new File("G:/zzz.dcm");
   input = new FileInputStream(file);
   b = new byte[(int) file.length()];
   input.read(b);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  init();
 }
 public static void init(){
  System.out.println("b.length="+b.length);
  for (int i =0;i<10000;i++) {
    System.out.print(Integer.toHexString(b[i]));  
   if (i%16==15) {
    System.out.println();
   }else{
    System.out.print(", ");
   }
  }
 }
}

(because the file is too There are 130,000 bytes, so for the sake of demonstration, we only loop 10,000 times. Read the first 10,000 bytes)

The above code is very common, which is to read the file stream into a byte array. Use Integer.toHexString(b[i]) to convert it to hexadecimal.

A problem occurred.

After running:

#Compare the hexadecimal list opened by the editor above. The red letter should be a6, but it is not ffffffa6 is printed.

Find the problem

The pen calculation error byte position is 140. Printsystem.out.pritln(b[140]);The result is -90. Why -90?.

Pushing backwards to a6 and converting it into decimal should be 166.

Okay, I found the problem. 166+90=256 This is no coincidence. One problem overlooked is that the maximum value of the byte array is only 127. Therefore, when the array read in the file is greater than 127, an error will occur when reading the byte array.

Solution


##

public static void init(){
  System.out.println("b.length="+b.length);
  for (int i =0;i<10000;i++) {
  if (b[i]<0) {
    int temp=b[i]+256;
    System.out.print(Integer.toHexString(temp));
   }else{
    System.out.print(Integer.toHexString(b[i]));
   }

   if (i%16==15) {
    System.out.println();
   }else{
    System.out.print(", ");
   }
  }
 }

Summary

The above is the detailed content of Example analysis of Java parsing DICOM diagram to obtain hexadecimal data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn