-1

I want to extract a set of images from a PNG file using their background position (like we do in a CSS file).

The main PNG file is List Of Flags and I want to get each country flag separately.

Is there any way in Android to extract those flags programmatically?

Thanks,

enter image description here

Rachik Abidi
  • 156
  • 8

1 Answers1

0


1. Load file into Bitmap

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
// selected_photo.setImageBitmap(bitmap);   // if you need


2. Access each pixel array

int dstWidth = width of the flag rectangle;
int dstHeight = height of the flag rectangle;
int pixelArray[] = new int[dstWidth * dstHeight];

int startX = start X position of your flag;
int endX = end X position of your flag;
int startY = start Y position of your flag;
int endX = end Y position of your flag;

bitmap.getPixels(pixelArray, 0, startX, startX, startY, dstWidth, dstHeight);  


3. Create a small Bitmap with array

Bitmap flag = Bitmap.createBitmap(dstWidth, dstHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(pixelArray);
Suhyeon Lee
  • 569
  • 4
  • 18
  • Thanks @Suhyeon Lee, I thought there is another solution rather than using the Bitmap decoding method, but it's alright, I will see if this solution works for me ;) – Rachik Abidi Aug 30 '16 at 12:07