You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Cornerstone在JavaScript中解析Axios获取的DICOM文件?

Hey, I see the issue here — those \u0000 escape characters mean Axios is treating your binary DICOM file like plain text, which mangles the raw binary data. You can't parse that corrupted string with Cornerstone, so let's fix this step by step:

Fixing DICOM Parsing with Axios + Cornerstone

1. First, Get the Correct Binary Data from Axios

Axios defaults to parsing responses as JSON or text, which breaks binary DICOM files. You need to force it to fetch the data as an ArrayBuffer (raw binary buffer) instead.

Here's how to adjust your Axios request:

import axios from 'axios';

async function fetchDicomBinary(url) {
  try {
    const response = await axios.get(url, {
      // Critical: Tell Axios to return raw binary data
      responseType: 'arraybuffer',
      // Optional: Add an Accept header to explicitly request DICOM
      headers: {
        'Accept': 'application/dicom'
      }
    });
    // response.data is now an ArrayBuffer with unmodified DICOM binary data
    return response.data;
  } catch (err) {
    console.error('Failed to fetch DICOM:', err);
    throw err;
  }
}

2. Parse the ArrayBuffer with Cornerstone

Once you have the valid binary data, you'll need cornerstone-wado-image-loader (a companion library for Cornerstone that handles DICOM parsing) along with dicom-parser.

First, install required dependencies (if using npm):

npm install cornerstone-core cornerstone-wado-image-loader dicom-parser

Then, parse and load the DICOM image:

import cornerstone from 'cornerstone-core';
import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader';
import dicomParser from 'dicom-parser';

// Initialize the loader with external dependencies
cornerstoneWADOImageLoader.external.cornerstone = cornerstone;
cornerstoneWADOImageLoader.external.dicomParser = dicomParser;

async function loadDicomImage(url) {
  // Step 1: Fetch the binary ArrayBuffer
  const dicomBuffer = await fetchDicomBinary(url);

  // Step 2: Parse the buffer into a Cornerstone-compatible image
  cornerstoneWADOImageLoader.loadImageFromArrayBuffer(dicomBuffer)
    .then(image => {
      // Success! This image object can be used with Cornerstone
      console.log('Parsed DICOM image:', image);
      
      // Example: Display it in a DOM element
      const viewerElement = document.getElementById('dicom-viewer');
      cornerstone.displayImage(viewerElement, image);
    })
    .catch(err => {
      console.error('Failed to parse DICOM:', err);
    });
}

Quick Troubleshooting Tips

  • Invalid DICOM File: If parsing fails, verify your DICOM file is valid (some files have non-standard formatting that breaks parsers).
  • Dependency Compatibility: Make sure your versions of cornerstone-core, cornerstone-wado-image-loader, and dicom-parser are compatible (stick to recent stable releases).
  • CORS Issues: If the DICOM is hosted on another domain, ensure the server has CORS enabled to allow your frontend to fetch the file.

内容的提问来源于stack exchange,提问作者rronan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:01:20