安卓:BitmapFactory.de codeByteArray给人像素化的位图 [英] Android: BitmapFactory.decodeByteArray gives pixelated bitmap

查看:249
本文介绍了安卓:BitmapFactory.de codeByteArray给人像素化的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,显示这是从Flickr下载的照片。我获得一个字节数组,而这又是从相关的Flickr网址读取位图对象,方法如下:

I am working on an Android app that displays photos which are downloaded from Flickr. I obtain a bitmap object from a byte array, which in turn is read from the relevant Flickr URL, as follows:

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方法的画布:

I then draw the bitmap onto a canvas in the onDraw method of a View object:

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint); 

问题是,所产生的画面像素化,我不明白为什么;我已经尝试了一些,没有运气的选择和油漆对象的变化。在我的应用程序显示的画面和画面的原始URL之间的差大致由以下证明:

The problem is that the resulting picture is pixelated and I can't figure out why; I have tried a number of variations of the opt and paint objects with no luck. The difference between the picture displayed in my app and the picture at the original URL is roughly demonstrated by the following:

看着如在云在左上角看到的差异。

Look e.g. at the clouds in the top-left corner to see the difference.

请注意,这是从项目资源加载并绘制以类似的方式显示就好了JPEG图片,即有没有像素。

Note that JPEG pictures which are loaded from the project resources and drawn in a similar way display just fine, i.e. have no pixelation.

任何人都可以给我一个提示,为什么发生这种情况?

Can anybody give me a hint as to why this is happening?

要详细一点,字节数组从Flickr获得如下:这是基于由罗曼盖伊照片媒体应用程序code:

To elaborate a little, the byte array is obtained from Flickr as follows; this is based on code from the Photostream app by Romain Guy:

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谷歌组这个问题变体

PS: I also posted a variant of this question on the android.developer Google group.

非常感谢您的建议 - 我现在真的很困惑!我确实如你所说,发现直接从下载的字节数组产生的图像确实是像素化。然而,这是从具体哪,我的电脑上访问时,是不是像素化相同的URL下载。下面是相应的Flickr网址:

Thanks a lot for your suggestion -- now I am really puzzled! I did as you suggested and found that the image resulting directly from the downloaded byte array is indeed pixelated. However, this is downloaded from exactly the same URL which, when accessed on my computer, is NOT pixelated. Here is the corresponding Flickr URL:

http://farm3.static.flickr.com/2678/4315351421_54e8cdb8e5.jpg

更奇怪的是,当我在模拟器,而不是我的手机(HTC Hero的)运行相同的应用程序,没有像素结构。

Even stranger, when I run the same app in the simulator rather than on my phone (a HTC Hero), there is no pixelation.

如何在地球上,这是可能的吗?

How on earth is this possible?

下面是code我用从一个URL加载一个位图 - 这是基于罗曼盖伊照片媒体应用程序,它结合了威尔的建议写的原始字节数组到文件:

Below is the code I use for loading a bitmap from a URL -- it is based on the Photostream app by Romain Guy, and it incorporates Will's suggestion to write the raw byte array to file:

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);
        }
    }
}

我是不是快要疯了吗? 最好, 迈克尔。

Am I going crazy here? Best, Michael.

推荐答案

而写的网址获取的原始字节为 /sdcard/tmp.jpg ,并查看您的PC。

Write the raw bytes fetched from the URL to /sdcard/tmp.jpg, and view on your PC.

JPEG图像融为一体pressed在8×8(或16×16)的瓷砖。在像素化为你描述它实际上是在这些瓦片,这表明坏的图像是JPEG是更积极地融为一体pressed比其他。

JPEG images are compressed in 8x8 (or 16x16) tiles. The 'pixelation' as you describe it is actually in these tiles, suggesting that the 'bad' image is a JPEG that is more aggressively compressed than the other.

所以我预期的实际问题是,在下载的图像是一个非常低质量的版本,例如一个是用于缩图/ preVIEW使用情况。

So I'd anticipate that the actual issue is that the image being downloaded is a very low-quality version, e.g. one intended for thumbnailing/preview use-cases.

这篇关于安卓:BitmapFactory.de codeByteArray给人像素化的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆