我在这个问题上被困了 3 天。我得到了一个 Dicom 文件,我需要使用缓冲阅读器对其进行解析,该阅读器从文档的标题和图像的原始数据返回一些信息。之后,我在 raw 上应用 LUT 将字节转换为灰度,然后将其放入Bitmap.create
. 它非常适合小图像,但现在,我必须加载 13Mo 图像,不仅需要很长时间才能打开它(大约 20 秒),而且在应用 LUT int 位图方法时,Android 会抛出关于 Bitmap 的错误29052480-byte external allocation too large for this process.
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
。我知道有很多关于这个错误的线索,但就我而言,它有点原始,因为我只想打开一个图像(所以它不是关于堆叠很多位图)。我可以给你看一些代码:
刷新Bmp:
private void refreshBmp(int windowWidth, int windowCentre) {
int[] colorArray = process.transformBuffer(myDicomObject.getRawData(),
myDicomObject.isInverted(), windowWidth, windowCentre,
myDicomObject.getnBits());
Bitmap bmp = Bitmap.createBitmap(colorArray,
myDicomObject.getColumns(), myDicomObject.getRows(),
Bitmap.Config.ARGB_8888);
dicomImageView.setImageBitmap(bmp);
}
这叫我的LUT:
public int[] transformBuffer(int[] rawData, boolean inverted,
int windowWidth, int windowCenter, int nBits) {
System.gc();
int min = windowCenter - (windowWidth/2);
int max = windowCenter + (windowWidth/2);
int intGrayscale = (int) Math.pow(2, nBits);
int intDivisionFactor = nBits-8;
double dmin = (double) min;
double dmax = (double) max;
double doubleGrayScale = (double) intGrayscale;
int rawDataLength = rawData.length;
int[] resultBuffer = new int[rawDataLength];
lutBuffer = new int[intGrayscale];
if(inverted){
for(int i = 0 ; i < min ; i++){
lutBuffer[i] = 255;
}
for(int i = min ; i < max ; i++){
double value = doubleGrayScale * ((i - dmin + 1) / (dmax - dmin + 1));
lutBuffer[i] = (int) (doubleGrayScale - value) >> intDivisionFactor;
}
for(int i = max ; i < intGrayscale ; i++){
lutBuffer[i] = 0;
}
}else{
for(int i = 0 ; i < min ; i++){
lutBuffer[i] = 0;
}
for(int i = min ; i < max ; i++){
double value = ((i - dmin + 1) / (dmax - dmin + 1));
lutBuffer[i] = (int) (value) << intDivisionFactor;
}
for(int i = max ; i < intGrayscale ; i++){
lutBuffer[i] = 255;
}
}
for(int i = 0 ; i < rawDataLength ; i++){
int colorValue = lutBuffer[rawData[i]];
resultBuffer[i] = Color.argb(255, colorValue, colorValue, colorValue);
}
System.out.println(resultBuffer.length);
return resultBuffer;
}
希望有人知道一种节省内存分配的方法,特别是 LUT 方法。