Androidプログラミングの写真関連コードまとめ
本論文の例はAndroidプログラミングの画像関連コードをまとめたものである。皆さんに参考にしてあげます。具体的には以下の通りです。
1.Bitmapを文字列に変換する:
1.Bitmapを文字列に変換する:
/**
* @param
* @return
*/
public static String bitmapToString(Bitmap bitmap) {
// Bitmap
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
2.文字列をBitmapに変換する:
/**
* @param string
* @return
*/
public static Bitmap stringToBitmap(String string) {
// Bitmap
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
3.BitmapをDrawableに変換する:
/**
* @param bitmap Bitmap
* @return Drawable Drawable
*/
public static Drawable bitmapToDrawable(Bitmap bitmap) {
if (bitmap == null)
return null;
if (160 != bitmap.getDensity()) {
bitmap.setDensity(160);
}
return new BitmapDrawable(bitmap);
}
写真リソースIDからDrawableオブジェクトを取得する:
/**
* @param context
* @param id ID
* @return Drawable
*/
public static Drawable resourceToDrawable(Context context,int id) {
return null == context ? null : bitmapToDrawable(BitmapFactory.decodeResource(context.getResources(), id));
}
byte配列変換Drawbleオブジェクト:
/**
* @param bytes byte
* @return drawble
*/
public static Drawable byteArrayToDrawable(byte[] bytes) {
return null == bytes ? null : bitmapToDrawable(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
4.Drawableをbitmapに変換する:
/**
* Drawble Bitmap
* @param drawable drawble
* @return bitmap
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
return null == drawable ? null : ((BitmapDrawable) drawable).getBitmap();
}
5.byte配列変換Bitmapオブジェクト:
/**
* @param bytes byte
* @return bitmap
*/
public static Bitmap byteArrayToBitmap(byte[] bytes) {
return null == bytes ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
6.画像を無色にして、階調画像(旧式画像)を返します。
/**
* @param bitmap bitmap
* @return Bitmap
*/
public static Bitmap toGrayscale(Bitmap bitmap) {
int width,height;
height = bitmap.getHeight();
width = bitmap.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
return bmpGrayscale;
}
7.画像を拡大縮小する:
/**
* @param url
* @param requireSize
* @return Bitmap
*/
public static Bitmap getScaleImage(String url,int requireSize) {
BitmapFactory.Options o = new BitmapFactory.Options();
// , ,
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(url, o);
int width_tmp = o.outWidth,height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < requireSize || height_tmp / 2 < requireSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp = BitmapFactory.decodeFile(url, o2);
return bmp;
}
8.画像の倒影を取得しながら、グラデーションの効果を適用します。
/**
* @param bitmap
* @return Bitmap
*/
public static Bitmap createMirro(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int shadow_height = 15;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// shadow effect
int alpha = 0x00000000;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int index = y * width + x;
int r = (pixels[index] >> 16) & 0xff;
int g = (pixels[index] >> 8) & 0xff;
int b = pixels[index] & 0xff;
pixels[index] = alpha | (r << 16) | (g << 8) | b;
}
if (y >= (height - shadow_height)) {
alpha = alpha + 0x1F000000;
}
}
// invert effect
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int y = 0; y < height; y++) {
bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);
}
return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);
}
9.写真をSDCardに保存する:
/**
* @param imagePath
* @param bm bitmap
*/
public static void saveImgToLocal(String imagePath, Bitmap bm) {
if (bm == null || imagePath == null || "".equals(imagePath)) {
return;
}
File f = new File(imagePath);
if (f.exists()) {
return;
} else {
try {
File parentFile = f.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
f.createNewFile();
FileOutputStream fos;
fos = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
f.delete();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
f.delete();
}
}
}
10.SDカードから画像を取得する:
/**
* @param imagePath SDCard
* @return bitmap
*/
public static Bitmap getImageFromLocal(String imagePath) {
File file = new File(imagePath);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
file.setLastModified(System.currentTimeMillis());
return bitmap;
}
return null;
}
11.画像圧縮処理:
/**
* , OOM 。
* 。
* @param context
* @param resId Id
* @param reqWidth
* @param reqHeight
* @return
*/
public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
/*
* ,inJustDecodeBounds true,
* bitmap , bitmap ,
*/
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resId, options);
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = inSampleSize;
// inSampleSize
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(context.getResources(), resId, options);
}
12.利用可能メモリの最大値を取得する(Appはメモリを使用してこの値を超えるとOutOfMemory異常を引き起こす):
private int getMaxMemoryForApp() {
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
return maxMemory;
}
13.画像を円に裁断する:
/**
* Bitmap
* @param bitmap
* @return
*/
public static Bitmap circlePic(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int r = width < height ? width/2:height/2;// , ,
Bitmap outBitmap = Bitmap.createBitmap(r*2, r*2, Bitmap.Config.ARGB_8888);// 2r Bitmap
Canvas canvas = new Canvas(outBitmap);
final int color =0xff424242;
final Paint paint = new Paint();
/**
* ,
* :
* 1. w < h , (0, (h-w)/2) , (w, (h+w)/2) 10
* 2. w > h , ((w-h)/2, 0) , ((w+h)/2, h) 10
*/
final Rect rect = new Rect( width < height ? 0 : (width-height)/2, width < height ? (height-width)/2 - 10 : -10,
width < height ? width : (width+height)/2, (width < height ? (height+width)/2 - 10: height - 10));
// , canvas
final Rect rect2 = new Rect( 0, 0, r*2, r*2);
// ,
final RectF rectF = new RectF(rect2);
// canvas
paint.setAntiAlias(true);
canvas.drawARGB(0,0,0,0);
paint.setColor(color);
// , r, rect2
canvas.drawRoundRect(rectF, r, r, paint);
//
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// canvas
canvas.drawBitmap(bitmap, rect, rect2, paint);
return outBitmap;
}
}
ここで述べたように、皆さんのAndroidプログラムの設計に役に立ちます。