URLConnection の InputStream を使用して、URL から大きな jpeg ファイルをロードしています。目標は、画像データで int[] を取得することです。これは、ビットマップを使用してさらに使用するよりも効率的であるためです。ここには 2 つのオプションがあります。
1 つ目は、Bitmap オブジェクトを作成し、結果を int[] にコピーすることです。これは私のアプリケーションでは機能しますが、画像データが int[] 画像にコピーされるため、ロード時に完全な画像が 2 回メモリ内に存在します。
Bitmap full = BitmapFactory.decodeStream(conn.getInputStream());
full.getPixels(image, 0, width, 0, 0, width, height);
メモリを節約するために、このプロセスを BitmapRegionDecoder を使用してタイル状に実行しようとしています。
int block = 256;
BitmapRegionDecoder decoder = BitmapRegionDecoder.
newInstance(conn.getInputStream(), false);
Rect tileBounds = new Rect();
// loop blocks
for (int i=0; i<height; i+=block) {
// get vertical bounds limited by image height
tileBounds.top = i;
int h = i+block<height ? block : height-i;
tileBounds.bottom = i+h;
for (int j=0; j<width; j+=block) {
// get hotizontal bounds limited by image width
tileBounds.left = j;
int w = j+block<width ? block : width-j;
tileBounds.right = j+w;
// load tile
tile = decoder.decodeRegion(tileBounds, null);
// copy tile in image
int index = i*width + j;
tile.getPixels(image, index, width, 0, 0, w, h);
}
}
技術的にはこれが機能し、int[] イメージで完全なイメージを取得します。また、タイルが画像にさりげなく挿入されています。
今私の問題。2 番目の方法では、ある種の奇妙な市松模様の歪みのある画像が生成されます。ピクセルは、わずかに暗いかわずかに明るい間で交互に表示されます。BitmapRegionDecoder は jpeg をサポートするはずであり、BitmapFactory.decodeStream は問題ありません。ここで何が問題なのですか?