我正在开发一个Android应用程序,它可以显示从Flickr下载的照片。我从一个字节数组中获取一个位图对象,然后从相关的Flickr读取它,如下所示:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);然后在视图对象的onDraw方法中将位图绘制到画布上:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint); 问题是,产生的图片是像素化的,我不知道为什么;我尝试了一些变化的opt和绘画对象没有运气。在我的应用程序中显示的图片和在原始URL上显示的图片之间的区别大致由以下内容显示:
不好的图像,请参见左上角http://homepages.inf.ed.ac.uk/s0677975/bad.jpg中的像素化
很好的图片,这是预期的结果http://homepages.inf.ed.ac.uk/s0677975/good.jpg
例如,看看左上角的云,看看差别。
请注意,从项目资源加载并以类似方式绘制的JPEG图片显示得很好,即没有像素化。
有人能告诉我为什么会发生这种事吗?
详细说明一下,字节数组是从Flickr获得的,如下所示;它基于Romain Guy的Photostream应用程序中的代码:
InputStream in = new BufferedInputStream(url.openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();PS:我还在android.developer Google组上发布了这个问题的变体。
非常感谢你的建议--现在我真的很困惑!我按照您的建议做了,发现直接由下载的字节数组生成的图像实际上是像素化的。但是,这是从完全相同的URL下载的,当在我的计算机上访问时,该URL不是像素化的。下面是对应的Flickr URL:
54e8cdb8e5.jpg
更奇怪的是,当我在模拟器中运行相同的应用程序而不是在我的手机上运行时(一个HTC英雄),就没有像素化了。
这到底是怎么可能的?
下面是我用于从URL加载位图的代码--它基于Romain Guy的Photostream应用程序,它结合了Will的建议,将原始字节数组写入文件:
Bitmap loadPhotoBitmap(URL url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
FileOutputStream fos = new FileOutputStream("/sdcard/photo-tmp.jpg");
BufferedOutputStream bfs = new BufferedOutputStream(fos);
in = new BufferedInputStream(url.openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
bfs.write(data, 0, data.length);
bfs.flush();
BitmapFactory.Options opt = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not load photo: " + this, e);
} finally {
closeStream(in);
closeStream(out)
closeStream(bfs);
}
return bitmap;
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not close stream", e);
}
}
}我在这里要疯了吗?最好的迈克尔。
发布于 2010-02-02 12:14:03
将从URL获取的原始字节写入/sdcard/tmp.jpg,并在您的PC上查看。
JPEG图像以8x8 (或16x16)块压缩。你所描述的“像素化”实际上是在这些瓷砖中,这意味着“坏”图像是一种比其他图像更具有攻击性的JPEG。
因此,我预计实际的问题是下载的图像是一个非常低质量的版本,例如一个用于拇指钉/预览用例的版本。
发布于 2010-02-02 16:28:55
好的,我终于明白了:看来我的移动网络通过图像压缩来节省带宽。
因此,从我的手机下载的图片比从我的电脑下载的图片质量要低。
这对我的应用程序来说是件令人沮丧的事情,但我不认为我能做些什么。叹一口气。
再次感谢您的投入!
最好的迈克尔。
发布于 2012-01-29 22:51:56
有些版本的Android在Bitmap类中有一个bug,并在某些操作中将Bitmap转换为RGB_565。这将在类似于您图片上的工件中显示出来。这也解释了蓝天的带状。另外,请记住,android试图通过在加载甚至编译资源文件时将其转换为rgb_565来“优化”映像。看看:http://android.nakatome.net/2010/04/bitmap-basics.html
https://stackoverflow.com/questions/2183808
复制相似问题