DICOM入门(四)——Android读取DCM文件图片

NDK和CMake 从入门到放弃

本章将介绍如何使用imebra读取展示DCM文件中的图片

Imebra

Imebra是用C++编写的DICOM库,支持多平台编译Linux, Windows, OS-X, iOS, Android。支持DICOM数据解析、支持压缩图片格式、支持DICOMDIR、支持Dicom3 & NEMA标准

源码获取

imebra_4_0_11_1
doc|
examples|
    |-changeTranferSyntax
    |-dicom2jpeg
    |-dicomdirItems
library|
   |-implementation
   |-include
   |-objectivec
   |-src
tests|
wrappers|
    |-javaWrapper
    |-pythonWrapper

examples 可以生成三个小程序
library c++源码
wrappers 包含python java的部分代码

创建项目

1.创建一个Android Studio项目在imebra目录下
2.为该项目添加一个Android Library

Moudle.jpeg
项目结构.jpeg

3.往Library添加创建src/main创建cpp文件夹

WechatIMG100.jpeg

4.将wrapper/javaWrapper/src/所有文件复制到Library的src/main/java下
5.将wrapper/java_wrapper.cxx复制到Library的/src/main/java
6.Library目录下创建CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.4.1)

include_directories(../../library/include)
aux_source_directory(../../library/implementation/ IMPL_SRCS)
aux_source_directory(../../library/src/ DIR_SRCS)
aux_source_directory(src/main/cpp/ JNI_SRC)

link_directories(libs)


add_library( # Sets the name of the library.
             imebra

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${JNI_SRC}
             ${IMPL_SRCS}
             ${DIR_SRCS}
             )



find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )


target_link_libraries( # Specifies the target library.
                       imebra

                       # Links the target library to the log library
                       # included in the NDK.

                       ${log-lib}
                     )

include_directories 加载头文件
aux_source_directory加载目录下的所有文件
find_library 加载指定库
target_link_libraries 生成动态库

7.修改Library的build.gradle

android {
   
    defaultConfig {
       
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -DIMEBRA_USE_JAVA -DIMEBRA_MEMORY_POOL_MAX_SIZE=4000000 -fexceptions -pthread"

            }
            ndk {

                abiFilters'armeabi', 'armeabi-v7a'
            }
        }

    }
    
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }

    }
}

cppFlags 使用c11标准 编译IMEBRA转码部分使用JAVA转码 设置内存池大小

8.com.imebra.imebraJNI类添加

static {
    System.loadLibrary("imebra");
  }

9.加载图片

public void loadDICOM(String name){
        String newPath = getCacheDir()+"/"+name;
        copyFilesFassets(this,name,newPath);

        DataSet dataSet = CodecFactory.load(newPath);
        Image image = dataSet.getImage(0);
        Log.d("height",""+image.getHeight());
        Log.d("width",""+image.getWidth());

        long height = image.getHeight();
        long width = image.getWidth();
        com.imebra.TransformsChain chain = new com.imebra.TransformsChain();

        if(com.imebra.ColorTransformsFactory.isMonochrome(image.getColorSpace()))
        {
            // Allocate a VOILUT transform. If the DataSet does not contain any pre-defined
            //  settings then we will find the optimal ones.
            VOILUT voilutTransform = new VOILUT();

            // Retrieve the VOIs (center/width pairs)
            VOIs vois = dataSet.getVOIs();

            // Retrieve the LUTs
            List<LUT> luts = new ArrayList<LUT>();
            for(long scanLUTs = 0; ; scanLUTs++)
            {
                try
                {
                    luts.add(dataSet.getLUT(new com.imebra.TagId(0x0028,0x3010), scanLUTs));
                }
                catch(Exception e)
                {
                    break;
                }
            }

            if(!vois.isEmpty())
            {
                voilutTransform.setCenterWidth(vois.get(0).getCenter(), vois.get(0).getWidth());
            }
            else if(!luts.isEmpty())
            {
                voilutTransform.setLUT(luts.get(0));
            }
            else
            {
                voilutTransform.applyOptimalVOI(image, 0, 0, width, height);
            }

            chain.addTransform(voilutTransform);
        }

        com.imebra.DrawBitmap draw = new com.imebra.DrawBitmap(chain);

// Ask for the size of the buffer (in bytes)
        long requestedBufferSize =width*height*4;

        byte buffer[] = new byte[(int)requestedBufferSize]; // Ideally you want to reuse this in subsequent calls to getBitmap()
        ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);

// Now fill the buffer with the image data and create a bitmap from it
        draw.getBitmap(image, drawBitmapRGBA, 4, buffer);
        Bitmap renderBitmap = Bitmap.createBitmap((int)image.getWidth(), (int)image.getHeight(), Bitmap.Config.ARGB_8888);
        renderBitmap.copyPixelsFromBuffer(byteBuffer);


        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageBitmap(renderBitmap);
    }
1.dcm
2.dcm.jpeg
3.dcm.jpeg

获取Android项目源码
https://github.com/fishCoder/ImeBraAndroid

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,349评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,567评论 2 45
  • 有一种美在远方,在那蓝天白云之下。在余晖之下的山是那么的绿,水是那么的蓝,江中倒影美不胜收。江面上那别具一格的建筑...
    幸福的眼泪_52da阅读 170评论 0 1
  • 说到写作呢,心里真的没底。毕竟很长时间都不曾动过书本,平常的阅读什么的几乎很少。大部分都是看看新闻故事,翻翻小说。...
    风鱼微微阅读 165评论 0 1
  • 报名S写作训练营二期的课程,是为了弥补一期没报上的遗憾。不巧的是,二期的上课时间 正好赶上二胎的预产期...
    雨下太阳花阅读 146评论 1 1