Bitmap工具类

对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());
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬味的黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值