You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I saw something very similar in issue #1742 but there wasn't a solution. I'm using React + Vite. Currently, the page has a file input, which the wadouri.fileManage.add, imageLoader.loadImage and the viewport.getImageDatata works succesfully when i select the .dcm file, as some logs shows (check the images below). But to display the image, it only shows a black container. Edit: I also tried to communicate with a Flask server (which will be a crucial part of my project) that, for simplicity, only rotates the received image, and it worked. I was able to save the response image in .dcm format. So, the only problem is with the DICOM visualization.
import{useState,useRef,useEffect}from"react";importaxiosfrom"axios";import{initascoreInit,RenderingEngine,imageLoader,Types,Enums}from"@cornerstonejs/core";import{initasdicomImageLoaderInit,wadouri}from"@cornerstonejs/dicom-image-loader";import{Upload}from"lucide-react";coreInit();dicomImageLoaderInit();constrenderingEngineId="myRenderingEngine";constviewportId="dicomViewport";constHomePage=()=>{const[image,setImage]=useState<string|null>(null);const[dicomFile,setDicomFile]=useState<File|null>(null);constimageRef=useRef<HTMLDivElement>(null);constrenderingEngineRef=useRef<RenderingEngine|null>(null);useEffect(()=>{return()=>{if(renderingEngineRef.current){renderingEngineRef.current.destroy();}};},[]);consthandleFileSelect=async(event: React.ChangeEvent<HTMLInputElement>)=>{constfile=event.target.files?.[0];if(!file)return;console.log("Arquivo selecionado:",file);setDicomFile(file);constimageId=wadouri.fileManager.add(file);console.log("ID da imagem:",imageId);setImage(imageId);try{const_image=awaitimageLoader.loadImage(imageId);console.log("Imagem carregada:",_image);if(!imageRef.current){console.error("imageRef.current é null");return;}if(!renderingEngineRef.current){renderingEngineRef.current=newRenderingEngine(renderingEngineId);}constrenderingEngine=renderingEngineRef.current;constviewportInput={
viewportId,type: Enums.ViewportType.STACK,element: imageRef.current,}renderingEngine.enableElement(viewportInput);constviewport=renderingEngine.getViewport(viewportId)asTypes.IStackViewport;awaitviewport.setStack([imageId]);viewport.render();setTimeout(function(){constmetadata=viewport.getImageData();console.log('Metadados:',metadata);},5000);}catch(error){console.error("Erro ao carregar imagem DICOM:",error);}};constsendFileToServer=async()=>{if(!dicomFile)return;constformData=newFormData();formData.append("dicom",dicomFile);try{constresponse=awaitaxios.post("http://127.0.0.1:5000/upload",formData,{headers: {"Content-Type": "multipart/form-data"},responseType: "blob",// Expecting a binary response (DICOM file)});console.log("Resposta do servidor:",response.data);constblob=newBlob([response.data],{type: "application/dicom"});// const file = new File([blob], "rotated.dcm");// Create a downloadable URLconstdownloadUrl=URL.createObjectURL(blob);// Create a temporary link and trigger downloadconsta=document.createElement("a");a.href=downloadUrl;a.download="rotated_image.dcm";// Filename to savedocument.body.appendChild(a);a.click();document.body.removeChild(a);// Free memoryURL.revokeObjectURL(downloadUrl);}catch(error){console.error("Erro ao enviar arquivo DICOM:",error);}};return(<divclassName="bg-gray-300 h-screen w-full flex flex-col gap-8 items-center justify-center py-8"><divonClick={()=>document.getElementById("dicom-upload")?.click()}className="border-2 border-dashed rounded-lg cursor-pointer border-gray-400 h-[512px] w-[512px] transition-colors">{image ? (<divclassName="relative w-full h-full"><divref={imageRef}className="w-[512px] h-[512px]">{/* Cornerstone will render the DICOM image here */}</div><pclassName="text-sm text-gray-500 text-center mt-2">
Clique para mudar a imagem
</p></div>) : (<divclassName="flex flex-col items-center justify-center gap-2 w-full h-full"><Uploadsize={40}/><pclassName="text-md font-semibold text-center text-gray-500">
Clique para selecionar imagem DICOM
</p></div>)}<inputid="dicom-upload"type="file"accept=".dcm"className="hidden"onChange={handleFileSelect}/></div><buttononClick={sendFileToServer}disabled={!dicomFile}className="py-1 text-lg w-18 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:bg-gray-400">
Enviar
</button></div>);};exportdefaultHomePage;
Steps to Reproduce
Create a React + TypeScript project using Vite
Follow the configuration setup outlined in the "Vite" section of the cornerstonejs docs
If you will also configure tailwind, add the following in index.css (layer base is necessary due some bugs like margin and padding that was not working without this):
Describe the Bug
I saw something very similar in issue #1742 but there wasn't a solution. I'm using React + Vite. Currently, the page has a file input, which the
wadouri.fileManage.add
,imageLoader.loadImage
and theviewport.getImageDatata
works succesfully when i select the.dcm
file, as some logs shows (check the images below). But to display the image, it only shows a black container.Edit: I also tried to communicate with a Flask server (which will be a crucial part of my project) that, for simplicity, only rotates the received image, and it worked. I was able to save the response image in
.dcm
format. So, the only problem is with the DICOM visualization.Steps to Reproduce
The current behavior
The image container is all black.
The expected behavior
It should display the DICOM image correctly.
OS
Windows 11
Node version
20.11.0
Browser
Chrome 132.0.6834.160
The text was updated successfully, but these errors were encountered: