共計 831 個字符,預計需要花費 3 分鐘才能閱讀完成。
將 Bitmap 轉成二進制數組,然后遍歷數組找出重復的像素點,進行去重操作。
以下是一個示例的 Java 代碼實現:
import java.util.HashSet;
import java.util.Set;
public class BitmapDuplicateRemoval {public static void main(String[] args) {int[][] bitmap = {{1, 2, 3, 4},
{5, 6, 7, 8},
{1, 2, 3, 4},
{9, 10, 11, 12}
};
int[][] result = removeDuplicates(bitmap);
for (int[] row : result) {for (int pixel : row) {System.out.print(pixel + " ");
}
System.out.println();}
}
public static int[][] removeDuplicates(int[][] bitmap) {Set seen = new HashSet();
int height = bitmap.length;
int width = bitmap[0].length;
int[][] result = new int[height][width];
for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {int pixel = bitmap[i][j];
if (!seen.contains(pixel)) {result[i][j] = pixel;
seen.add(pixel);
}
}
}
return result;
}
}
輸出結果為:
1 2 3 4
5 6 7 8
0 0 0 0
9 10 11 12
在上述代碼中,我們使用了一個 HashSet 來存儲已經出現過的像素點,如果一個像素點在 HashSet 中不存在,那么我們將其添加到 HashSet 中,并將其賦值給結果數組。如果一個像素點已經存在于 HashSet 中,則將其賦值為 0,表示去重。最后返回結果數組。
丸趣 TV 網 – 提供最優質的資源集合!
正文完