when i had to solve that problem i used a buffer...
private IntBuffer buffer;
public void toBuffer(File tempFile){
final Bitmap temp = BitmapFactory.decodeFile(imgSource
.getAbsolutePath());
buffer.rewind(); //setting the buffers index to '0'
temp.copyPixelsToBuffer(buffer);
}
then you can simply edit all pixels (int-values) on your buffer (as mentioned from https://stackoverflow.com/users/3850595/jordi-castilla) ...
...setting ARGB for 0xFFFFFFFF
(white&transparent) to 0x00??????
(any color would suit, it's transparent anyway)
so here's your code to edit transparenty on a buffer:
public void convert(){
for (int dy = 0; dy < imgHeight; dy ++){
for (int dx = 0; dx < imgWidth; dx ++ ){
int px = buffer.get();
int a = (0xFF000000 & px) >> 24;
int r = (0x00FF0000 & px) >> 16;
int g = (0x0000FF00 & px) >> 8;
int b = (0x000000FF & px);
int result = px;
if (px == 0xFFFFFFFF){ //only adjust alpha when the color is 'white'
a = 0xFF000000; //transparent?
int result = a | r | g | b;
}
int pos = buffer.position();
buffer.put(pos-1, result);
}
}
later you want to write back your image into an converted file:
public void copyBufferIntoImage(File tempFile) throws IOException {
buffer.rewind();
Bitmap temp = Bitmap.createBitmap(imgWidth, imgHeight,
Config.ARGB_8888);
temp.copyPixelsFromBuffer(buffer);
FileOutputStream out = new FileOutputStream(tempFile);
temp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
}
maybe you still want to know how to map a buffer?
public void mapBuffer(final File tempFile, long size) throws IOException {
RandomAccessFile aFile = new RandomAccessFile(tempFile, "rw");
aFile.setLength(4 * size); // 4 byte pro int
FileChannel fc = aFile.getChannel();
buffer = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size())
.asIntBuffer();
}