Android Bitmap保存时背景变为黑色的问题

      之前写的一个Android程序,一直有个bug悬而未决:背景色原来为白色,可保存图片时却变成黑色。昨天又拿出来看了看,突然想到新建Bitmap对象时,默认变量应该和Java中其他新建变量或对象的情况类似,默认值为0。因此猜想新建一个Bitmap时,每个像素的值都是0,即黑色。于是创建一个每个像素点都是255(白色)的Bitmap就行了。

    部分代码如下。

    得到位图的方法:

 1 /**
 2      * 得到相应背景色的位图
 3  * @param width 位图的宽度  4  * @param height 位图的高度  5  * @param color 位图的背景色  6  * @return 该颜色的位图  7 */  8 public Bitmap getBitmapByColor(int width,int height,int color){  9  Bitmap newBitmap; 10 int[] colors=new int[width*height];//新建像素点数组,数组元素个数是位图的宽乘以高 11 for (int i=0;i<colors.length;i++){ 12 colors[i]=color;//将颜色赋值给每一个像素点 13  } 14 newBitmap= createBitmap(colors,width,height,Bitmap.Config.ARGB_8888); 15 return newBitmap; 16 }
View Code

    构造方法:

1 public DrawView(Context context, AttributeSet attributeSet) {
2         super(context, attributeSet); 3 int width = context.getResources().getDisplayMetrics().widthPixels;//得到屏幕的宽度 4 int height = context.getResources().getDisplayMetrics().heightPixels;//得到屏幕的高度 5 Bitmap bitmap=getBitmapByColor(width,height,Color.WHITE); 6 Canvas canvas = new Canvas(); 7  Canvas.setBitmap(bitmap); 8 }
View Code

    保存图片的方法:

 1 /**
 2      * 将图片保存在内存卡的pictures文件夹内
 3  * @param fileName 文件名  4  * @throws IOException  5 */  6 public void savePic(String fileName) throws IOException {  7 File file = new File("/sdcard/pictures/" + fileName + ".png");  8  file.createNewFile();  9 FileOutputStream fileOS = new FileOutputStream(file); 10 cacheBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS);//注意是PNG格式的。若设置为JPG格式,背景色会变黑 11  fileOS.flush(); 12  fileOS.close(); 13 }
View Code

     改成这样,运行后发现报错!进行断点调试,发现” canvas.setBitmap(bitmap);”出错了。在网上查了一下,原来Canvas对象在执行setBitmap方法时,首先判断这个位图是不是可变的,如果是不可变的,那么便不能执行setBitmap()方法。bitmap.isMutable()返回一个布尔值。如果bitmap是可变的,则返回true;反之返回false。将这条语句嵌入到代码中,logcat上显示bitmap确实是不可变的。因此得想办法将Bitmap对象变为可变才行。在网上查到,只有一种方法可行,那就是调用bitmap的copy()方法,拷贝一份给另一个Bitmap对象,copy()有一个参数,可以设置拷贝的一份是不是可变的。不过这样原来的Bitmap对象就没什么用了。

     因此其他方法不变,将构造方法改为:

1 public DrawView(Context context, AttributeSet attributeSet) {
2         super(context, attributeSet); 3 int width = context.getResources().getDisplayMetrics().widthPixels;//得到屏幕的宽度 4 int height = context.getResources().getDisplayMetrics().heightPixels;//得到屏幕的高度 5 Bitmap tempBitmap=getBitmapByColor(width,height,Color.WHITE); 6 Canvas canvas = new Canvas(); 7 Bitmap bitmap=tempBitmap.copy(tempBitmap.getConfig(),true);//true表示该bitmap对象是可变的;false则反之 8  canvas.setBitmap(bitmap); 9 }
View Code

    这样便运行成功了。

     其实还有一种方法,就是先执行createBitmap()方法,创建一个Bitmap对象。然后将这个Bitmap对象的像素点全部设置为想要的颜色。经测试发现,这样不会导致bitmap变为不可变的。

     部分代码如下。     

     设置位图背景色的方法:

 1 /**
 2      * 设置位图的背景色
 3  * @param bitmap 需要设置的位图  4  * @param color 背景色  5 */  6 public void setBitmapBGColor(Bitmap bitmap,int color){  7 for(int i=0;i<bitmap.getWidth();i++){  8 for(int j=0;j<bitmap.getHeight();j++){  9 bitmap.setPixel(i,j,color);//将bitmap的每个像素点都设置成相应的颜色 10  } 11  } 12 }
View Code

    构造方法:

1 public DrawView(Context context, AttributeSet attributeSet) {
2         super(context, attributeSet); 3 width = context.getResources().getDisplayMetrics().widthPixels;//得到屏幕的宽度 4 height = context.getResources().getDisplayMetrics().heightPixels;//得到屏幕的高度 5 Bitmap bitmap=createBitmap(width,height,Bitmap.Config.ARGB_8888); 6  setBitmapBGColor(bitmap,Color.WHITE); 7 Canvas canvas = new Canvas(); 8  canvas.setBitmap(bitmap); 9 }
View Code

    保存图片的方法不变。以上所有的构造方法只是给出了部分代码。

 

转载于:https://www.cnblogs.com/cestlavie/p/4162127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Android Bitmap 保存为 BMP 格式,可以使用以下步骤: 1. 创建一个 BITMAPFILEHEADER 结构体,用于保存 BMP 文件头信息。 2. 创建一个 BITMAPINFOHEADER 结构体,用于保存位图信息头。 3. 计算位图像素数据的大小,用于分配内存。 4. 将 Android Bitmap 转换为 RGB 格式的位图像素数据。 5. 将位图像素数据按照 BMP 文件格式存储到内存中。 6. 将 BITMAPFILEHEADER 和 BITMAPINFOHEADER 写入 BMP 文件开头。 7. 将位图像素数据写入 BMP 文件中。 以下是一个示例代码,用于将 Android Bitmap 保存为 BMP 格式: ```java import android.graphics.Bitmap; import android.graphics.Color; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class BitmapUtils { // BMP 文件头 private static class BITMAPFILEHEADER { public char bfType[] = {'B', 'M'}; public int bfSize; public short bfReserved1; public short bfReserved2; public int bfOffBits; } // BMP 位图信息头 private static class BITMAPINFOHEADER { public int biSize; public int biWidth; public int biHeight; public short biPlanes; public short biBitCount; public int biCompression; public int biSizeImage; public int biXPelsPerMeter; public int biYPelsPerMeter; public int biClrUsed; public int biClrImportant; } // 将 Android Bitmap 保存为 BMP 格式 public static boolean saveBitmapAsBmp(Bitmap bitmap, String filename) { // 获取位图信息 int width = bitmap.getWidth(); int height = bitmap.getHeight(); // 计算位图像素数据大小 int data_size = width * height * 3; // 创建 BMP 文件头 BITMAPFILEHEADER file_header = new BITMAPFILEHEADER(); file_header.bfSize = 14 + 40 + data_size; file_header.bfReserved1 = 0; file_header.bfReserved2 = 0; file_header.bfOffBits = 14 + 40; // 创建 BMP 位图信息头 BITMAPINFOHEADER info_header = new BITMAPINFOHEADER(); info_header.biSize = 40; info_header.biWidth = width; info_header.biHeight = height; info_header.biPlanes = 1; info_header.biBitCount = 24; info_header.biCompression = 0; info_header.biSizeImage = data_size; info_header.biXPelsPerMeter = 0; info_header.biYPelsPerMeter = 0; info_header.biClrUsed = 0; info_header.biClrImportant = 0; // 将 Android Bitmap 转换为 RGB 格式的位图像素数据 int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); byte[] data = new byte[data_size]; int i = 0; for (int y = height - 1; y >= 0; y--) { for (int x = 0; x < width; x++) { int pixel = pixels[y * width + x]; data[i++] = (byte) Color.blue(pixel); data[i++] = (byte) Color.green(pixel); data[i++] = (byte) Color.red(pixel); } } // 写入 BMP 文件 try { FileOutputStream fos = new FileOutputStream(new File(filename)); ByteBuffer buffer = ByteBuffer.allocate(14 + 40 + data_size); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putChar('B'); buffer.putChar('M'); buffer.putInt(file_header.bfSize); buffer.putShort(file_header.bfReserved1); buffer.putShort(file_header.bfReserved2); buffer.putInt(file_header.bfOffBits); buffer.putInt(info_header.biSize); buffer.putInt(info_header.biWidth); buffer.putInt(info_header.biHeight); buffer.putShort(info_header.biPlanes); buffer.putShort(info_header.biBitCount); buffer.putInt(info_header.biCompression); buffer.putInt(info_header.biSizeImage); buffer.putInt(info_header.biXPelsPerMeter); buffer.putInt(info_header.biYPelsPerMeter); buffer.putInt(info_header.biClrUsed); buffer.putInt(info_header.biClrImportant); buffer.put(data); fos.write(buffer.array()); fos.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } } ``` 这个函数需要传入一个 Android Bitmap 对象和一个保存文件名,函数会将 Android Bitmap 保存为 BMP 格式,并返回一个布尔值表示保存是否成功。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值