在android中将图像转换为透明图像
我需要将图像背景转换为透明图像,并将该图像保存在外部存储器中。下面的代码我很累,但图像变为黑色背景,而不是透明的 指定pix[index]=Color.TRANSPARENT正确吗在android中将图像转换为透明图像,android,bitmap,android-bitmap,Android,Bitmap,Android Bitmap,我需要将图像背景转换为透明图像,并将该图像保存在外部存储器中。下面的代码我很累,但图像变为黑色背景,而不是透明的 指定pix[index]=Color.TRANSPARENT正确吗 public Bitmap createTransparentBitmapFromBitmap( int replaceThisColor) { try { // furniture1.JPG is the image to be converted.
public Bitmap createTransparentBitmapFromBitmap( int replaceThisColor) {
try {
// furniture1.JPG is the image to be converted.
InputStream is = getAssets().open("furniture1.JPG");
Bitmap bitmap = BitmapFactory.decodeStream(is);
if (bitmap != null) {
int picw = bitmap.getWidth();
int pich = bitmap.getHeight();
int[] pix = new int[picw * pich];
bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
for (int y = 0; y < pich; y++) {
// from left to right
for (int x = 0; x < picw; x++) {
int index = y * picw + x;
int r = (pix[index] >> 16) & 0xff;
int g = (pix[index] >> 8) & 0xff;
int b = pix[index] & 0xff;
if (pix[index] == replaceThisColor) {
pix[index] = Color.TRANSPARENT;
} else {
break;
}
}
// from right to left
for (int x = picw - 1; x >= 0; x--) {
int index = y * picw + x;
int r = (pix[index] >> 16) & 0xff;
int g = (pix[index] >> 8) & 0xff;
int b = pix[index] & 0xff;
if (pix[index] == replaceThisColor) {
pix[index] = Color.TRANSPARENT;
} else {
break;
}
}
}
Bitmap bm = Bitmap.createBitmap(pix, picw, pich,Bitmap.Config.ARGB_8888);
Log.i("DATABASE", "mutable: "+bm.isMutable());
is.close();
return bm;
}
return null;
}catch(Exception ex) {
Log.i("DATABASE", "Problem updating picture", ex);
}
return null;
}
// Creates an transparent image and save to SD card.
public void createTransparent() {
// Replace WHITE color to TRANSPARENT color
Bitmap bm = createTransparentBitmapFromBitmap(Color.WHITE);
//Save the converted image to SD card
boolean externalMounted= isExternalStorageWritable();
File myDir=null;
if(externalMounted)
{
String root = Environment.getExternalStorageDirectory().toString();
myDir = new File(root + "/VirtualHome/Ikea","aaa1.jpeg");
OutputStream os;
try {
os = new FileOutputStream(myDir);
bm.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
}catch(IOException ex) {
Log.i("DATABASE", "Problem updating picture", ex);
}
}
}
公共位图createTransparentBitmapFromBitmap(int replaceThisColor){
试一试{
//furniture1.JPG是要转换的图像。
InputStream=getAssets().open(“furniture1.JPG”);
位图位图=BitmapFactory.decodeStream(is);
if(位图!=null){
int picw=bitmap.getWidth();
int pich=bitmap.getHeight();
int[]pix=新的int[picw*pich];
获取像素(像素,0,像素,0,0,像素,像素);
对于(int y=0;y>16)和0xff;
int g=(pix[index]>>8)&0xff;
int b=pix[索引]&0xff;
if(pix[索引]==replaceThisColor){
pix[索引]=颜色。透明;
}否则{
打破
}
}
//从右到左
对于(int x=picw-1;x>=0;x--){
int index=y*picw+x;
int r=(pix[index]>>16)和0xff;
int g=(pix[index]>>8)&0xff;
int b=pix[索引]&0xff;
if(pix[索引]==replaceThisColor){
pix[索引]=颜色。透明;
}否则{
打破
}
}
}
位图bm=Bitmap.createBitmap(pix、picw、pich、Bitmap.Config.ARGB_8888);
Log.i(“数据库”,“可变:”+bm.isMutable());
is.close();
返回bm;
}
返回null;
}捕获(例外情况除外){
Log.i(“数据库”,“问题更新图片”,ex);
}
返回null;
}
//创建透明图像并保存到SD卡。
public void createTransparent(){
//将白色替换为透明色
位图bm=createTransparentBitmapFromBitmap(颜色.白色);
//将转换后的图像保存到SD卡
布尔值externalMounted=isExternalStorageWritable();
文件myDir=null;
如果(外部安装)
{
String root=Environment.getExternalStorageDirectory().toString();
myDir=新文件(root+“/VirtualHome/Ikea”,“aaa1.jpeg”);
输出流;
试一试{
os=新文件输出流(myDir);
bm.compress(Bitmap.CompressFormat.JPEG,100,操作系统);
os.flush();
os.close();
}捕获(IOEX异常){
Log.i(“数据库”,“问题更新图片”,ex);
}
}
}
@MorrisonChang:你的代码很累,结果是一样的。白色背景输入图片转换成黑色背景。我的回答是前面的问题,可能有答案。你应该用你尝试过的内容更新你的问题。您可能需要清理示例代码,因为我在其中看到了三个文件名,这使读者感到困惑。此外,您正在使用ARGB_4444,由于性能差,它在API 13中被弃用@莫里森昌:你的代码很累,结果是一样的。白色背景输入图片转换成黑色背景。我的回答是前面的问题,可能有答案。你应该用你尝试过的内容更新你的问题。您可能需要清理示例代码,因为我在其中看到了三个文件名,这使读者感到困惑。此外,您正在使用ARGB_4444,由于性能差,它在API 13中被弃用。