いつも質問ばっかりすいません
Bitmapをある一定の大きさで読み込みたくて、サイズが大きすぎる画像ののやり方は乗っていたのですが、画像の大きさが表示したい大きさより小さい時、
int src_width =bmp.getWidth(); int src_height = bmp.getHeight(); float xScale = (float) reqWidth / bmp.getWidth() ; float yScale = (float) reqHeight / bmp.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(xScale, yScale); Bitmap b = Bitmap.createBitmap(bmp, 0, 0, src_width, src_height, matrix, true);
このようにスケーリングするとしか書いていないのですが、このやり方だとかなり小さい画像を大きくした時にサイズが大きくなりすぎてしまってメモリリークしてしまいます。
そこで下のようにまず読み込んだあと、「その画像を指定した大きさにした時画像サイズがいくつになるか」を計算してからそれを指定した画像サイズ(ここでは2MB)まで小さくするためにもういちどoptions.inSampleSizeを設定しなおして読み込むという方法をとったところ、確かに画質が悪くなっているのは間違いないのですが、なぜか最後にcreateしたbitmapはサイズが変わっていないんです。
Bitmap bmp = null; try { bmp = BitmapFactory.decodeStream(cr.openInputStream(uri),null, options); int originalSize=bmp.getRowBytes() * bmp.getHeight(); int reqTimes=(MaxWidth / bmp.getWidth())*(MaxWidth / bmp.getWidth()); options.inSampleSize = (int) ((originalSize*reqTimes)/ (float) 2000000); Log.d("message",""+(int) ((originalSize*reqTimes)/ (float) 2000000)); bmp=null; bmp = BitmapFactory.decodeStream(cr.openInputStream(uri),null, options); bmp = BitmapFactory.decodeStream(cr.openInputStream(uri),null, options); } catch (FileNotFoundException e) { e.printStackTrace(); }
bitmapのサイズというのは画像の大きさを変えないかぎり画質を悪くしても変わらないのですか?
画像の大きさはそのままで画質悪くなってもいいのでサイズを変える方法はないのでしょうか...
回答2件
あなたの回答
tips
プレビュー