// Load the grayscale image into a Bitmap Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.grayscale); // Create a new bitmap with the same dimensions as the original Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888); // Set the pixel colors of the new bitmap to the grayscale values of the original image int[] pixels = new int[image.getWidth() * image.getHeight()]; image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight()); for (int i = 0; i < pixels.length; i++) { int gray = Color.red(pixels[i]); pixels[i] = Color.argb(255, gray, gray, gray); } newBitmap.setPixels(pixels, 0, newBitmap.getWidth(), 0, 0, newBitmap.getWidth(), newBitmap.getHeight()); // Use the new bitmap in your application
// Load the image into a Bitmap Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image); // Create a new bitmap with the same dimensions as the original Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888); // Apply a box blur filter to the image int[] pixels = new int[image.getWidth() * image.getHeight()]; image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight()); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int sumR = 0; int sumG = 0; int sumB = 0; int count = 0; for (int dy = -1; dy <= 1; dy++) { for (int dx = -1; dx <= 1; dx++) { int nx = x + dx; int ny = y + dy; if (nx >= 0 && nx < image.getWidth() && ny >= 0 && ny < image.getHeight()) { int idx = ny * image.getWidth() + nx; sumR += Color.red(pixels[idx]); sumG += Color.green(pixels[idx]); sumB += Color.blue(pixels[idx]); count++; } } } int idx = y * image.getWidth() + x; int r = sumR / count; int g = sumG / count; int b = sumB / count; pixels[idx] = Color.rgb(r, g, b); } } newBitmap.setPixels(pixels, 0, newBitmap.getWidth(), 0, 0, newBitmap.getWidth(), newBitmap.getHeight()); // Use the new bitmap in your applicationBoth examples use the android.graphics package library.