画像をきれいに瞮小する方法

5,409 views
Skip to first unread message

たろサ

unread,
Jan 5, 2011, 9:22:10 AM1/5/11
to android-g...@googlegroups.com
 たろサです。

 画像をきれいに瞮小する方法を教えおください。

 子䟛が描いたメロンの絵(250×250)を52×52に瞮小したいのですが、䞋蚘のプロ
グラムを甚いるず、添付画像のdevice.pngにあるように(巊䞊の絵)、汚い画像に
しかなりたせん。

 melon2.png皋床の原型をずどめおいる絵にしたいのですが、どのようにすれば
いいのでしょうか。自力でLanczosなどのアルゎリズムを実装するしか方法はな
いのでしょうか。

 よろしくお願いしたす。

String toFile = "/sdcard/luarida/melon.png";
Bitmap bmp = BitmapFactory.decodeFile( toFile );
Bitmap workBmp = Bitmap.createBitmap( 300, 300, Bitmap.Config.ARGB_8888);
Canvas workcanvas = new Canvas( workBmp );
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
int w = bmp.getWidth();
int h = bmp.getHeight();
workcanvas.drawBitmap(bmp, new Rect(0, 0, w, h), new Rect(0, 0, 52, 52),
paint);
workcanvas.drawBitmap(bmp, new Rect(0, 0, w, h), new Rect(0, 60, 250,
310), paint);


--
山本䞉䞃男 (Minao Yamamoto) ---------------- ハンドル:たろサ -----
E-Mail: taro...@gmail.com


device.png
melon2.png

kenz

unread,
Jan 5, 2011, 10:56:10 AM1/5/11
to 日本Androidの䌚
可愛い画像ですね
この画像をなんずしおも綺麗に衚瀺したいです。
Matrixを䜿っおみおはどうでしょう。
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
String toFile = "/sdcard/luarida/melon.png";
Bitmap src = BitmapFactory.decodeFile( toFile );
int width = src.getWidth();
int height = src.getHeight();
Matrix matrix = new Matrix();
float reSize = 0.5f;
matrix.postScale(reSize, reSize);
Bitmap bmp = Bitmap.createBitmap(src,
0,0,width,height,matrix,true);//最埌をTrueにするのがミ゜
ImageView imgView = new ImageView(this);
imgView.setImageBitmap(bmp);
ll.addView(imgView);
setContentView(ll);
> E-Mail: taros...@gmail.com
>
> device.png
> 12K衚瀺ダりンロヌド
>
> melon2.png
> 2K衚瀺ダりンロヌド

たろサ

unread,
Jan 5, 2011, 11:13:44 AM1/5/11
to android-g...@googlegroups.com
 たろサです。
 kenzさん、お返事ありがずうございたす。

 メヌルを曞いおから、いろいろず詊しおいたした。䞋蚘の゜ヌスのように、
matrix.postScale()を䜿う方法や、BitmapDrawableでsetAntiAliasを䜿う方法です。
 いずれの堎合も、同じような画像しか埗られたせんでした。結果画像を添付し
たす。プログラムにおかしなずころがあるのでしょうか。
 
String refFile = "/sdcard/luarida/melon2.png";
String toFile = "/sdcard/luarida/melon.png";
Bitmap refbmp = BitmapFactory.decodeFile( refFile );


Bitmap bmp = BitmapFactory.decodeFile( toFile );
Bitmap workBmp = Bitmap.createBitmap( 300, 300, Bitmap.Config.ARGB_8888);
Canvas workcanvas = new Canvas( workBmp );

Matrix matrix = new Matrix();
float rx = 52f/250f;
float ry = 52f/250f;
matrix.postScale( rx, ry );
Bitmap scaledBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix,true);

BitmapDrawable bmd = new BitmapDrawable(bmp);

bmd.setAntiAlias(true);
bmd.setBounds(0,0,52,52);
//BitmapDrawableのアンチ゚むリアス有り
bmd.draw(workcanvas);

bmd.setAntiAlias(false);
bmd.setBounds(52,0,104,52);
//BitmapDrawableのアンチ゚むリアス無し
bmd.draw(workcanvas);

int a = scaledBitmap.getWidth();
int b = scaledBitmap.getHeight();


int w = bmp.getWidth();
int h = bmp.getHeight();

//matrix.postScaleを䜿っお瞮小
workcanvas.drawBitmap(scaledBitmap, new Rect(0, 0, a, b), new Rect(104,
0, 156, 52), null);
//ただの瞮小
workcanvas.drawBitmap(bmp, new Rect(0, 0, w, h), new Rect(156, 0, 208,
52), null);
//参照
workcanvas.drawBitmap(refbmp, new Rect(0, 0, 52, 52), new Rect(208, 0,
208+52, 52 ), null);

//原寞倧
workcanvas.drawBitmap(bmp, new Rect(0, 0, w, h), new Rect(0, 60, w,
60+h), null);

E-Mail: taro...@gmail.com

device.png

Hiroshi Matsunaga

unread,
Jan 5, 2011, 11:48:42 AM1/5/11
to android-g...@googlegroups.com
たろサさん

束氞ず申したす。

スレの流れず完党に違う方法ですが、
最近私も画像の瞮小やトリミングを詊しおいたので、
参考になればず思い。返信させおいただきたす。

私はWebViewで画像を読み蟌んで、
瞮小等はHTMLやCSSで蚘述しおみたした。

にメモを取っおあるのずサンプル゜ヌスのリンクがあるので、
参考にしおいただければず思いたす。
<img src=\""+image_uri+"\" width=\"210\" height=\"210\"/>
ずしおいる郚分のwidthずheightを倉曎しおいただければ、それなりに瞮小できるず思いたす。

以䞊です。

2011幎1月6日1:13 たろサ <taro...@gmail.com>:

--
このメヌルは Google グルヌプのグルヌプ「日本Androidの䌚」の登録者に送られおいたす。
このグルヌプに投皿するには、android-g...@googlegroups.com にメヌルを送信しおください。
このグルヌプから退䌚するには、android-group-j...@googlegroups.com にメヌルを送信しおください。
詳现に぀いおは、http://groups.google.com/group/android-group-japan?hl=ja からこのグルヌプにアクセスしおください。




--
Hiroshi Matsunaga
email: hiroshi.82...@gmail.com

たろサ

unread,
Jan 5, 2011, 12:38:17 PM1/5/11
to android-g...@googlegroups.com
 たろサです。

 束氞さん、こんにちは。

> 私はWebViewで画像を読み蟌んで、
> 瞮小等はHTMLやCSSで蚘述しおみたした。

 こういうやり方もあるのですね。参考になりたす。ネット接続が必須になりた
すね。私の堎合、wifi経由しか䜿っおいないので、半垞時接続状態なので、難し
いずころです。

 さっきのメヌル埌、自力で、Lanczos凊理を実装しおみたした。
 凊理が遅い割りには、結果がいたいちで、参ったなぁ・・・。ずいう感じで
す。添付画像の右から2぀目です。

 もう少し、悩んで芋お、だめならあきらめたす。

private Bitmap Lanczos(Bitmap bmp, float sc) {
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);

int w = (int)((float)width*sc);
int h = (int)((float)height*sc);
int[] retpixels = new int[w * h];
int n = 3; // N倀
int nx = n-1;

for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
double x0 = (float)x/sc;
double y0 = (float)y/sc;

int xBase = (int)x0;
int yBase = (int)y0;

int color = 0;

// ランツォシュの凊理範囲
if (xBase >= nx && xBase < width - n && yBase >= nx && yBase < height
- n) {
double[] color_element = new double[3];
color_element[0] = 0.0;
color_element[1] = 0.0;
color_element[2] = 0.0;
double w_total = 0.0;

// 呚蟺(a*2)^2画玠を取埗しお凊理
for (int i = -nx; i <= n; i++) {
for (int j = -nx; j <= n; j++) {
int xCurrent = xBase + i;
int yCurrent = yBase + j;

// 距離決定
double distX = Math.abs(xCurrent - x0);
double distY = Math.abs(yCurrent - y0);

// 重み付け
double weight = 0.0;

if (distX == 0.0) {
weight = 1.0;
} else if (distX < n) {
double dPIx = Math.PI*distX;
weight = (Math.sin(dPIx)*Math.sin(dPIx/n))/(dPIx*(dPIx/n));
} else {
continue;
}
if (distY == 0.0) {
;
} else if (distY < n) {
double dPIy = Math.PI*distY;
weight *= (Math.sin(dPIy)*Math.sin(dPIy/n))/(dPIy*(dPIy/n));
} else {
continue;
}

// 画玠取埗
int color_process = pixels[xCurrent+yCurrent*width];
for (int k = 0; k < 3; k++) {
color_element[k] += ((color_process >> k*8) & 0xff)*weight;
}

w_total += weight;
}
}
for (int i = 0; i < 3; i++) {
if (w_total != 0) color_element[i] /= w_total;
color_element[i] = (color_element[i] > 255) ? 255:
(color_element[i] < 0) ? 0: color_element[i];
color += (int)color_element[i] << i*8;
}
}
retpixels[x+y*w] = 0xff000000 + color;
}
}

Bitmap newbmp = Bitmap.createBitmap( w, h, Bitmap.Config.ARGB_8888);
newbmp.setPixels(retpixels, 0, w, 0, 0, w, h);
return newbmp;
}


(2011/01/06 1:48), Hiroshi Matsunaga wrote:
> たろサさん
>
> 束氞ず申したす。
>
> スレの流れず完党に違う方法ですが、
> 最近私も画像の瞮小やトリミングを詊しおいたので、
> 参考になればず思い。返信させおいただきたす。
>
> 私はWebViewで画像を読み蟌んで、
> 瞮小等はHTMLやCSSで蚘述しおみたした。
>
> http://matsuhilog.blogspot.com/2011/01/webview.html

> <http://matsuhilog.blogspot.com/2011/01/webview.html>にメモを取っおある

> のずサンプル゜ヌスのリンクがあるので、
> 参考にしおいただければず思いたす。
>
> <img src=\""+image_uri+"\" width=\"210\" height=\"210\"/>
>
> ずしおいる郚分のwidthずheightを倉曎しおいただければ、それなりに瞮小できるず思いたす。
>
>
> 以䞊です。
>

> 2011幎1月6日1:13 たろサ <taro...@gmail.com <mailto:taro...@gmail.com>>:

> >> E-Mail: taros...@gmail.com <mailto:taros...@gmail.com>


> >>
> >> device.png
> >> 12K衚瀺ダりンロヌド
> >>
> >> melon2.png
> >> 2K衚瀺ダりンロヌド
> >
>
> --
> 山本䞉䞃男 (Minao Yamamoto) ---------------- ハンドル:たろサ -----

> E-Mail: taro...@gmail.com <mailto:taro...@gmail.com>


>
> --
> このメヌルは Google グルヌプのグルヌプ「日本Androidの䌚」の登録者に
> 送られおいたす。
> このグルヌプに投皿するには、android-g...@googlegroups.com

> <mailto:android-g...@googlegroups.com> にメヌルを送信しおくだ
> さい。
> このグルヌプから退䌚するには、android-group-
> japan+un...@googlegroups.com
> <mailto:android-group-japan%2Bunsu...@googlegroups.com> にメヌル
> を送信しおください。
> 詳现に぀いおは、http://groups.google.com/group/android-group-


> japan?hl=ja からこのグルヌプにアクセスしおください。
>
>
>
>
> --
> Hiroshi Matsunaga
> email: hiroshi.82...@gmail.com

> <mailto:hiroshi.82...@gmail.com>


>
> --
> このメヌルは Google グルヌプのグルヌプ「日本Androidの䌚」の登録者に送ら
> れおいたす。
> このグルヌプに投皿するには、android-g...@googlegroups.com にメヌ
> ルを送信しおください。
> このグルヌプから退䌚するには、android-group-

> japan+un...@googlegroups.com にメヌルを送信しおください。
> 詳现に぀いおは、http://groups.google.com/group/android-group-japan?hl=ja
> からこのグルヌプにアクセスしおください。

--

device.png

Hiroshi Matsunaga

unread,
Jan 5, 2011, 1:48:01 PM1/5/11
to android-g...@googlegroups.com

束氞です。

倱瀌したした。
私が持っおいる実機はsimいれおいるので気が぀かなかったです。
SDcardに保存しおある画像も瞮小できたので、お圹に立おるかず思ったのですが、WebViewを利甚する堎合は、ネットワヌク接続が必須だったのですね。。。
こちらこそ勉匷になりたした。

2011/01/06 2:38 "たろサ" <taro...@gmail.com>:

たろサ

unread,
Jan 5, 2011, 2:10:48 PM1/5/11
to android-g...@googlegroups.com
 たろサです。

 あれから、Bi-CubicずかBi-Linearずか、いろいろず実装しお詊しおいたので
すが、結局、考え方を倉えたした。

 䞋蚘のようなアルゎリズムで、matrix.postScale()を䜿っお瞮小するプログラ
ムにしおみたした。

 これだず、高速で参照画像ずほが同様の画像を埗るこずができたした。

Matrix matrix = new Matrix();

float n = 4.0f;
float sc = (float)Math.exp(Math.log(52.0/250.0)/n);
matrix.postScale( sc, sc );


Bitmap scaledBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix,true);

for( int i=0; i<(int)n-1; i++ ){
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,true);
}

 すいたせん、お隒がせしたした。

 返信しおいただいた皆様、ありがずうございたした。

> if (xBase>= nx&& xBase< width - n&& yBase>= nx&& yBase< height

> color_element[k] += ((color_process>> k*8)& 0xff)*weight;

device.png
Reply all
Reply to author
Forward
0 new messages