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
3.往Library添加创建src/main创建cpp文件夹
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 luts = new ArrayList();
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);
}
获取Android项目源码
https://github.com/fishCoder/ImeBraAndroid