2013年8月17日土曜日

BMP拡大回転反転などの画像処理クラス

色々と使える画像処理クラスを公開。

呼び出しサンプル
BitmapManerger bm =new BitmapManerger(getApplicationContext());

Bitmap bmp =bm.yomitori("512.png", 50);

//Bitmap bmp2= bm.syukusyou(bmp, 1f, 0.5f);
Bitmap bmp2 =bm.kaiten(bmp, 90);

bmp= bm.iro(bmp,0xffff0000);


//bmp=bm.hanten(bmp2);

Bitmap bmp3 =bm.gousei(bm.hairetu(bmp, bmp2));


iv.setImageBitmap(bmp3);

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

public class BitmapManerger {

Context cn;

public BitmapManerger(Context c) {
cn = c;
}

// 配列ゼロ板をキャンパスサイズにする。添え字が大きいほど上位レイヤー
public Bitmap gousei(Bitmap[] bmp) {
// ARGB_8888,RGB_565 ARGB_4444 RGB_565
Bitmap rbmp = Bitmap.createBitmap(bmp[0].getWidth(),
bmp[0].getHeight(), Bitmap.Config.ARGB_4444);//アルファチャンネルが必要
Canvas canvas = new Canvas(rbmp);



for (int i = 0; i < bmp.length; i++) {
canvas.drawBitmap(bmp[i], 0, 0, (Paint) null);
}

return rbmp;
}

public Bitmap yomitori(String pass,int size) {

InputStream is = null;

Bitmap bmp = null;

BitmapFactory.Options imageOptions = new BitmapFactory.Options();
imageOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, imageOptions);

float imageScaleWidth = (float)imageOptions.outWidth / size;

BitmapFactory.Options imageOptions2 = new BitmapFactory.Options();
imageOptions2.inSampleSize=(int) imageScaleWidth;
//Bitmap.Config.ARGB_4444
imageOptions2.inPreferredConfig=Bitmap.Config.ARGB_4444;
try {

// assets フォルダ読み取り
is = cn.getResources().getAssets().open(pass);
//
bmp= BitmapFactory.decodeStream(is, null, imageOptions2);

//bmp[i] = BitmapFactory.decodeStream(is);


} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}

return bmp;
}
// 画像のパス配列をビットマップ配列に
public Bitmap[] yomitori(String[] pass,int size) {

InputStream is = null;

Bitmap[] bmp = new Bitmap[pass.length];

BitmapFactory.Options imageOptions = new BitmapFactory.Options();
imageOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, imageOptions);

float imageScaleWidth = (float)imageOptions.outWidth / size;

BitmapFactory.Options imageOptions2 = new BitmapFactory.Options();
imageOptions2.inSampleSize=(int) imageScaleWidth;
imageOptions2.inPreferredConfig=Bitmap.Config.ARGB_4444;

try {

for (int i = 0; i < pass.length; i++) {
// assets フォルダ読み取り
is = cn.getResources().getAssets().open(pass[i]);
//
bmp[i] = BitmapFactory.decodeStream(is, null, imageOptions2);

//bmp[i] = BitmapFactory.decodeStream(is);
}

} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}

return bmp;
}

/ 縦横比率を変えずに高さ縮小
public Bitmap syukusyou(Bitmap bmp, float ww, float hh) {

Bitmap rbmp;

Bitmap cbmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(cbmp);

Matrix matrix = new Matrix();

matrix.postScale(ww, hh);

// リサイズ画像
rbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
matrix, true);

int poji = cbmp.getHeight() - rbmp.getHeight();
canvas.drawBitmap(rbmp, 0, poji, (Paint) null);

return cbmp;
}
//ok 
public Bitmap hanten(Bitmap bmp) {

Matrix matrix = new Matrix();

// 上下反転
// matrix.preScale(1, -1);
// 左右反転
matrix.preScale(-1, 1);
Bitmap rbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, false);

return rbmp;
}

// 画像を回転 (画像、回転) 時計回り 90で右回転
public Bitmap kaiten(Bitmap bmp, int inint) {

Matrix mat = new Matrix();
mat.postRotate(inint);

Bitmap rbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
return rbmp;
}

// 色を変更する
public Bitmap iro(Bitmap bmpImage, int inint) {

if (bmpImage == null) {
return null;
}

int w = bmpImage.getWidth();
int h = bmpImage.getHeight();
int initpixels[] = new int[(bmpImage.getWidth() * bmpImage.getHeight())];
bmpImage.getPixels(initpixels, 0, bmpImage.getWidth(), 0, 0,
bmpImage.getWidth(), bmpImage.getHeight());

// bmpImage.recycle();

for (int xx = 0; xx < (w * h) - 1; xx++) {

// 0xFFFFFF00
initpixels[xx] = (initpixels[xx] & inint);

}

Bitmap rBmp = Bitmap.createBitmap(initpixels, w, h,
Bitmap.Config.ARGB_4444);// (initpixels, 0, width, 0, 0, width,
// height);

initpixels = null;

return rBmp;

}

// 任意のタイミングでメモリ解放を
public void kaihou(Bitmap[] bmp) {
for (int i = 0; i < bmp.length; i++) {
bmp[0].recycle();
}
}
複数のBMPを配列へ
public Bitmap[] hairetu(Bitmap bmp1,Bitmap bmp2){
Bitmap[] bmp = new Bitmap[2];
bmp[0]=bmp1;
bmp[1]=bmp2;
return bmp;
}
public Bitmap[] hairetu(Bitmap bmp1,Bitmap bmp2,Bitmap bmp3){
Bitmap[] bmp = new Bitmap[3];
bmp[0]=bmp1;
bmp[1]=bmp2;
bmp[2]=bmp3;
return bmp;
}
public Bitmap[] hairetu(Bitmap bmp1,Bitmap bmp2,Bitmap bmp3,Bitmap bmp4){
Bitmap[] bmp = new Bitmap[4];
bmp[0]=bmp1;
bmp[1]=bmp2;
bmp[2]=bmp3;
bmp[3]=bmp4;
return bmp;
}
public Bitmap[] hairetu(Bitmap bmp1,Bitmap bmp2,Bitmap bmp3,Bitmap bmp4,Bitmap bmp5){
Bitmap[] bmp = new Bitmap[5];
bmp[0]=bmp1;
bmp[1]=bmp2;
bmp[2]=bmp3;
bmp[3]=bmp4;
bmp[4]=bmp5;
return bmp;
}

}

0 件のコメント:

コメントを投稿