我想通过使用位图显示 dicom 图像的负片,我这样做了
public static Bitmap getNegativeImage(Bitmap img) {
int w1 = img.getWidth();
int h1 = img.getHeight();
// int value[][] = new int[w1][h1];
Bitmap gray = Bitmap.createBitmap(
w1, h1, img.getConfig());
int value, alpha, r, g, b;
for (int i = 0; i < w1; i++) {
for (int j = 0; j < h1; j++) {
value = img.getPixel(i, j); // store value
alpha = getAlpha(value);
r = 255 - getRed(value);
g = 255 - getGreen(value);
b = 255 - getBlue(value);
value = createRGB(alpha, r, g, b);
gray.setPixel(i, j, value);
}
}
return gray;
}
public static int createRGB(int alpha, int r, int g, int b) {
int rgb = (alpha << 24) + (r << 16) + (g << 8) + b;
return rgb;
}
public static int getAlpha(int rgb) {
return (rgb >> 24) & 0xFF;
}
public static int getRed(int rgb) {
return (rgb >> 16) & 0xFF;
}
public static int getGreen(int rgb) {
return (rgb >> 8) & 0xFF;
}
public static int getBlue(int rgb) {
return rgb & 0xFF;
}
Run Code Online (Sandbox Code Playgroud)
但是反转(负片)图像变黑,再次单击时会显示原始图像,但不会显示反转图像。
问候沙迪亚 UM
我不明白为什么你的代码不起作用,但这应该:
private static final int RGB_MASK = 0x00FFFFFF;
public Bitmap invert(Bitmap original) {
// Create mutable Bitmap to invert, argument true makes it mutable
Bitmap inversion = original.copy(Config.ARGB_8888, true);
// Get info about Bitmap
int width = inversion.getWidth();
int height = inversion.getHeight();
int pixels = width * height;
// Get original pixels
int[] pixel = new int[pixels];
inversion.getPixels(pixel, 0, width, 0, 0, width, height);
// Modify pixels
for (int i = 0; i < pixels; i++)
pixel[i] ^= RGB_MASK;
inversion.setPixels(pixel, 0, width, 0, 0, width, height);
// Return inverted Bitmap
return inversion;
}
Run Code Online (Sandbox Code Playgroud)
它创建位图的可变副本(并非所有位图都是),反转所有像素的 RGB 部分,保持 alpha 完好无损
编辑 我知道为什么你的代码不起作用:你假设像素是 AARRGBB
| 归档时间: |
|
| 查看次数: |
3280 次 |
| 最近记录: |