对Bitmap的一些操作,包括旋转、压缩、截屏、保存等等。
public final class BitmapOperator {
public static byte[] convertBitmap2GrayArray(Bitmap img) {
byte[] theBytes = null;
/* 得到位图的宽高 */
int width = img.getWidth();
int height = img.getHeight();
/* 取得位图的像素点 */
int[] pixels = new int[width * height];
img.getPixels(pixels, 0, width, 0, 0, width, height);
/* 定义结果数据数组 */
theBytes = new byte[width * height / 2];
/* 定义循环中用到的变量,节约内存和时间 */
int x, y, k;
int pixel, r, g, b;
for (y = 0; y < height; y++) {
for (x = 0, k = 0; x < width; x++) {
// 依次取得像素点
pixel = pixels[y * width + x];
// 得到rgb
r = (pixel >> 16) & 0xFF;
g = (pixel >> 8) & 0xFF;
b = pixel & 0xFF;
/* 每两行存为一行 */
if (x % 2 == 1) {
theBytes[k + y * width / 2] = (byte) (theBytes[k + y
* width / 2] | ((r * 299 + g * 587 + b * 114 + 500) / 1000) & 0xf0);
k++;
} else {
theBytes[k + y * width / 2] = (byte) (theBytes[k + y
* width / 2] | (((r * 299 + g * 587 + b * 114 + 500) / 1000) >> 4) & 0x0f);
}
}
}
if (img != null && !img.isRecycled()) {
img.recycle();
}
return theBytes;
}
/**
* 旋转图片
*
* @param bmp
* @param degrees
* @return
*/
public static Bitmap rotateBitmap(Bitmap bmp, float degrees) {
Bitmap bitmap = null;
try {
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
/* 压缩到指定宽高 */
public static Bitmap getBitmapFromFile(String pathName, int width,
int height) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap src = BitmapFactory.decodeFile(pathName, opts);
opts.outWidth = width;
opts.outHeight = height;
opts.inJustDecodeBounds = false;
src = BitmapFactory.decodeFile(pathName, opts);
int w = src.getWidth();
int h = src.getHeight();
float scaleWidth = ((float) width) / w;
float scaleHeight = ((float) height) / h;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bmp = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);
return bmp;
}
/**
* 保存图片灰度数据到文件
*
* @param path
*/
/*
* private void saveImageBytesFile(String path) { Bitmap bmp =
* getBitmapFromFile(path, 480, 800); Bitmap img = rotateBitmap(bmp, -90);
*
* byte[] buffer = jni_convertIMG2GreyArray(img); if (buffer == null ||
* buffer.length <= 0) { return; } int length = buffer.length; byte[] data =
* new byte[length / 2]; for (int i = 0; i < length / 2; i++) { data[i] =
* (byte) ((buffer[i * 2] << 4) & 0xf0 + (buffer[i * 2 + 1] & 0x0f)); }
*
* byte[] buffer = convertBitmap2GrayArray(img); if (buffer == null ||
* buffer.length <= 0) { return; } FileOutputStream fos = null; try { File
* file = getFile(); fos = new FileOutputStream(file, false);
* fos.write(buffer); fos.flush(); } catch (FileNotFoundException e) { //
* TODO Auto-generated catch block e.printStackTrace(); } catch (IOException
* e) { // TODO Auto-generated catch block e.printStackTrace(); } finally {
* try { if (fos != null) fos.close(); } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } } }
*/
/**
* 得到文件路径
*
* @return
*/
private File getFile() {
File file = new File("/sdcard/EpdiMedia/screen.txt");
try {
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdCardExist) {
String fileName = "E";
String sdPath = Environment.getExternalStorageDirectory()
.getPath();
File tempFile = new File(sdPath + File.separator + fileName);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
file = new File(sdPath + File.separator + fileName
+ File.separator + "s" + ".txt");
/*
* if (file.exists()) { file.delete(); file.createNewFile(); }
*/
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
/**
* 截屏操作 把View转换成Bitmap并保存为文件 文件夹:gh文件名:以时间秒数命名 后缀名:.png
*
* @param view
*/
public static void screenShot(View view) {
view.setDrawingCacheEnabled(true);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
FileOutputStream fos = null;
try {
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdCardExist) {
String fileName = "gh";
String sdPath = Environment.getExternalStorageDirectory()
.getPath();
File tempFile = new File(sdPath + File.separator + fileName);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
File file = new File(sdPath + File.separator + fileName
+ File.separator + System.currentTimeMillis() + ".png");
if (file.exists()) {
file.delete();
file.createNewFile();
}
fos = new FileOutputStream(file);
if (fos != null) {
bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
}
}
} catch (Exception e) {
// Log.e(TAG, "cause for " + e.getMessage());
}
}
}