android bitmap 生成图片全黑 谁能告诉我为啥呢?

neu_sunlei 2014-07-15 05:11:12
如题,用的是二维码生成,在ImageView里面看的时候是正常的,然后保存到本地,是就黑色的,
保存代码如下


String file_name = VeDate.getNo(5) + ".png";
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(path, file_name);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();

...全文
6166 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿姨不可以嘛 2017-03-22
  • 打赏
  • 举报
回复
引用 20 楼 Mingyueyixi 的回复:
如果你保存成本地文件,背景真的变黑了,注意是真的!因为,有很多看图软件的背景是黑色的,当png透明时,当然也是黑色的,但是,换到ps里,或者其他看图软件中,就看出透明了。 如果真的变黑,原因九成是你的Bitmap对象在处理的过程中,有一个地方的颜色配置不对,以下生成Bitmap的方法: Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); Config.ARGB_8888是图片的配置,A代表透明度。 如果你的参数是不带A的,比如是Config.RGB_565,那么就肯定黑了。这和在window上,png另存为jpg,通常默认背景会变成白色的一样。
正解!!!
Mingyueyixi 2016-09-20
  • 打赏
  • 举报
回复
如果你保存成本地文件,背景真的变黑了,注意是真的!因为,有很多看图软件的背景是黑色的,当png透明时,当然也是黑色的,但是,换到ps里,或者其他看图软件中,就看出透明了。 如果真的变黑,原因九成是你的Bitmap对象在处理的过程中,有一个地方的颜色配置不对,以下生成Bitmap的方法: Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); Config.ARGB_8888是图片的配置,A代表透明度。 如果你的参数是不带A的,比如是Config.RGB_565,那么就肯定黑了。这和在window上,png另存为jpg,通常默认背景会变成白色的一样。
Mingyueyixi 2016-09-20
  • 打赏
  • 举报
回复
都在瞎扯!!png是无压缩,支持透明的图片,它没有像素的地方既不应该是黑色,也不应该是白色。因为,透明就是透明。。。png图片放在多图层中,透明的地方就能够看到底层图片。楼主把透明的地方改白色,根本就没有解决任何形式的问题。
lzc_a 2016-09-07
  • 打赏
  • 举报
回复
引用 14 楼 courysky 的回复:
[quote=引用 9 楼 neu_sunlei 的回复:] 找到bug的原因了,我在网上找到二维码生成图片的代码,拿过来直接用的,没有仔细检查,虽然在imageview中显示正常,视音频imageview中没有颜色的位置默认使用的是白色的替代的,但是保存成png后,没有颜色的位置使用的是黑色替代的,所以会看到生成的图片是全黑的,下面上代码


public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = BLACK;
				}else{
					pixels[y * width + x] = WHITE;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.RGB_565);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}


代码中

  pixels[y * width + x] = WHITE;
这段嗲吗是我自己后加上的,加上之后就能显示了,谢谢楼上各位朋友的好心帮助,
引用 9 楼 neu_sunlei 的回复:
找到bug的原因了,我在网上找到二维码生成图片的代码,拿过来直接用的,没有仔细检查,虽然在imageview中显示正常,视音频imageview中没有颜色的位置默认使用的是白色的替代的,但是保存成png后,没有颜色的位置使用的是黑色替代的,所以会看到生成的图片是全黑的,下面上代码


public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = BLACK;
				}else{
					pixels[y * width + x] = WHITE;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.RGB_565);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}


代码中

  pixels[y * width + x] = WHITE;
这段嗲吗是我自己后加上的,加上之后就能显示了,谢谢楼上各位朋友的好心帮助,
还是楼主威武啊,可以用。倒腾了两三个小时,就纳闷了[/quote] 楼主威武霸气,找到了正确原因。我也是同样的问题。感谢!顺便问下,楼主是如何想到这儿的问题的,给下思路
lzc_a 2016-09-07
  • 打赏
  • 举报
回复
引用 8 楼 neu_sunlei 的回复:
[quote=引用 7 楼 snlfqt 的回复:] 出现的问题可能原因: 1 没有任何数据写入到图片中 2 图片数据写入编码与读取编码不一致 qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);中的100指图片的质量,并非缩放比,android API对此描述如下: quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
有可能是你说的编码不一致的问题,怎么解决呢? 在imageView 显示的正常保存本低有问题[/quote] 楼主威武霸气,找到了正确原因。我也是同样的问题。感谢!顺便问下,楼主是如何想到这儿的问题的,给下思路
  • 打赏
  • 举报
回复
请问楼主,你的那个black和white是怎么定义的?
wswj1314 2016-07-14
  • 打赏
  • 举报
回复
真的是学习了啊
夜色蓝 2016-05-06
  • 打赏
  • 举报
回复
引用 9 楼 neu_sunlei 的回复:
找到bug的原因了,我在网上找到二维码生成图片的代码,拿过来直接用的,没有仔细检查,虽然在imageview中显示正常,视音频imageview中没有颜色的位置默认使用的是白色的替代的,但是保存成png后,没有颜色的位置使用的是黑色替代的,所以会看到生成的图片是全黑的,下面上代码


public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = BLACK;
				}else{
					pixels[y * width + x] = WHITE;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.RGB_565);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}


代码中

  pixels[y * width + x] = WHITE;
这段嗲吗是我自己后加上的,加上之后就能显示了,谢谢楼上各位朋友的好心帮助,
引用 9 楼 neu_sunlei 的回复:
找到bug的原因了,我在网上找到二维码生成图片的代码,拿过来直接用的,没有仔细检查,虽然在imageview中显示正常,视音频imageview中没有颜色的位置默认使用的是白色的替代的,但是保存成png后,没有颜色的位置使用的是黑色替代的,所以会看到生成的图片是全黑的,下面上代码


public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = BLACK;
				}else{
					pixels[y * width + x] = WHITE;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.RGB_565);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}


代码中

  pixels[y * width + x] = WHITE;
这段嗲吗是我自己后加上的,加上之后就能显示了,谢谢楼上各位朋友的好心帮助,
还是楼主威武啊,可以用。倒腾了两三个小时,就纳闷了
Young_DL 2015-11-13
  • 打赏
  • 举报
回复
感谢,感谢, 解决了我的一个问题
lianghuihaoKing 2015-06-14
  • 打赏
  • 举报
回复
感谢你,解决问题了
KchanY 2015-04-02
  • 打赏
  • 举报
回复
请问一下,怎么将bitmap图片保存呢,我已经做到生成了,希望告知一下
胖小了个花 2014-12-02
  • 打赏
  • 举报
回复
必须给力!亲测可用。
neu_sunlei 2014-07-31
  • 打赏
  • 举报
回复
找到bug的原因了,我在网上找到二维码生成图片的代码,拿过来直接用的,没有仔细检查,虽然在imageview中显示正常,视音频imageview中没有颜色的位置默认使用的是白色的替代的,但是保存成png后,没有颜色的位置使用的是黑色替代的,所以会看到生成的图片是全黑的,下面上代码


public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = BLACK;
				}else{
					pixels[y * width + x] = WHITE;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.RGB_565);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}


代码中

  pixels[y * width + x] = WHITE;
这段嗲吗是我自己后加上的,加上之后就能显示了,谢谢楼上各位朋友的好心帮助,
neu_sunlei 2014-07-31
  • 打赏
  • 举报
回复
引用 7 楼 snlfqt 的回复:
出现的问题可能原因: 1 没有任何数据写入到图片中 2 图片数据写入编码与读取编码不一致 qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);中的100指图片的质量,并非缩放比,android API对此描述如下: quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
有可能是你说的编码不一致的问题,怎么解决呢? 在imageView 显示的正常保存本低有问题
snlfqt 2014-07-18
  • 打赏
  • 举报
回复
出现的问题可能原因: 1 没有任何数据写入到图片中 2 图片数据写入编码与读取编码不一致 qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);中的100指图片的质量,并非缩放比,android API对此描述如下: quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
云上飞翔 2014-07-16
  • 打赏
  • 举报
回复
引用 楼主 neu_sunlei 的回复:
如题,用的是二维码生成,在ImageView里面看的时候是正常的,然后保存到本地,是就黑色的, 保存代码如下


String file_name = VeDate.getNo(5) + ".png";
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(path, file_name);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();

qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 这个参数怎么可能是100呢?是100就表示图片全没啦。 参数改为0,表示图片没有压缩,是100% 参数改为30,表示图片压缩到原来的70% 楼主参数是100,则表示图片压缩到原来的0%, 图片当然 没有啦。
neu_sunlei 2014-07-16
  • 打赏
  • 举报
回复
在我手机上缩略图是有显示的,打开就是黑色的了,小米2手机,保存的文件很小,2kb左右,不知道是不是没有数据,但是怎么能把这个东西保存成图片呢。
neu_sunlei 2014-07-16
  • 打赏
  • 举报
回复
引用 2 楼 unloserv 的回复:
Bitmap里没有数据就是黑的 楼主再仔细看看代码
在imageview里面可以显示,但是保存成图片不行,为啥呢
neu_sunlei 2014-07-16
  • 打赏
  • 举报
回复
引用 1 楼 sagittarius1988 的回复:
黑的说明没有数据,你改下压缩格式看看
我生成的bitmap在imageview里面是可以使用的,但是保存成文件就不行了,不知道为啥
unloserv 2014-07-15
  • 打赏
  • 举报
回复
Bitmap里没有数据就是黑的 楼主再仔细看看代码
加载更多回复(1)

80,355

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧