Java判断一个文件是否是DICOM文件

DICOM文件头中,包括128个00H字节的文件前言,和4字节(44,49,43,4D)的DICOM文件前缀,即DICM。我们可以通过魔数校验去判断文件是否是DICOM文件。

步骤如下:

public class DicomTest {

	public static void main(String[] args) throws Exception {
		// 存放DICOM文件128个00H字节的文件前言 + 4字节的DICOM文件前缀
		byte[] b = new byte[132];
		FileInputStream fis = new FileInputStream("F:/DicomImage/test.dcm");
		fis.read(b, 0, 132);
		IOUtils.closeQuietly(fis);
		// 存放DICOM文件魔数,结果得到的字节数组为[68, 73, 67, 77]
		byte[] dicomMagicNum = new byte[4];
		for (int i = 128; i < b.length; i++) {
			dicomMagicNum[i - 128] = b[i];
		}
		// 获取十六进制字符串,即44,49,43,4D
		System.out.println(byteToHexString(dicomMagicNum));
		// 获取字符串,即DICM
		System.out.println(new String(dicomMagicNum, "UTF-8"));
	}

	public static String byteToHexString(byte[] bytes) {
		if (ArrayUtils.isEmpty(bytes)) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			int hex = bytes[i] & 0xFF;
			String hs = Integer.toHexString(hex);
			if (hs.length() < 2) {
				sb.append(0);
			}
			sb.append(hs);
		}
		return sb.toString();
	}
}

 

通过dcm4che工具上传文件时,也会判断是否是dicom文件。

源码如下:

 private void guessTransferSyntax() throws IOException {
        byte[] b128 = new byte[128];
        byte[] buf = buffer;
        mark(132);
        int rlen = read(b128);
        if (rlen == 128) {
            read(buf, 0, 4);
            if (buf[0] == 'D' && buf[1] == 'I'
                    && buf[2] == 'C' && buf[3] == 'M') {
                preamble = b128.clone();
                if (!markSupported()) {
                    hasfmi = true;
                    tsuid = UID.ExplicitVRLittleEndian;
                    bigEndian = false;
                    explicitVR = true;
                    return;
                }
                mark(128);
                read(b128);
            }
        }
        if (rlen < 8
                || !guessTransferSyntax(b128, rlen, false)
                && !guessTransferSyntax(b128, rlen, true))
            throw new DicomStreamException(NOT_A_DICOM_STREAM);
        reset();
        hasfmi = TagUtils.isFileMetaInformation(
                ByteUtils.bytesToTag(b128, 0, bigEndian));
    }

    private boolean guessTransferSyntax(byte[] b128, int rlen, boolean bigEndian)
            throws DicomStreamException {
        int tag1 = ByteUtils.bytesToTag(b128, 0, bigEndian);
        VR vr = ElementDictionary.vrOf(tag1, null);
        if (vr == VR.UN)
            return false;
        if (ByteUtils.bytesToVR(b128, 4) == vr.code()) {
            this.tsuid = bigEndian ? UID.ExplicitVRBigEndianRetired 
                                   : UID.ExplicitVRLittleEndian;
            this.bigEndian = bigEndian;
            this.explicitVR = true;
            return true;
        }
        int len = ByteUtils.bytesToInt(b128, 4, bigEndian);
        if (len < 0 || 8 + len > rlen)
            return false;

        if (bigEndian)
            throw new DicomStreamException(IMPLICIT_VR_BIG_ENDIAN);

        this.tsuid = UID.ImplicitVRLittleEndian;
        this.bigEndian = false;
        this.explicitVR = false;
        return true;
    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值