`
zeroblue
  • 浏览: 45969 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android 图片操作(Bitmap)

 
阅读更多
/**
	 * 将多个Bitmap合并成一个图片。
	 * 
	 * @param int 将多个图合成多少列
	 * @param Bitmap... 要合成的图片
	 * @return
	 */
public static Bitmap combineBitmaps(int columns, Bitmap... bitmaps) {
		if (columns <= 0 || bitmaps == null || bitmaps.length == 0) {
			throw new IllegalArgumentException("Wrong parameters: columns must > 0 and bitmaps.length must > 0.");
		}
		int maxWidthPerImage = 0;
		int maxHeightPerImage = 0;
		for (Bitmap b : bitmaps) {
			maxWidthPerImage = maxWidthPerImage > b.getWidth() ? maxWidthPerImage : b.getWidth();
			maxHeightPerImage = maxHeightPerImage > b.getHeight() ? maxHeightPerImage : b.getHeight();
		}
		int rows = 0;
		if (columns >= bitmaps.length) {
			rows = 1;
			columns = bitmaps.length;
		} else {
			rows = bitmaps.length % columns == 0 ? bitmaps.length / columns : bitmaps.length / columns + 1;
		}
		Bitmap newBitmap = Bitmap.createBitmap(columns * maxWidthPerImage, rows * maxHeightPerImage, Config.RGB_565);

		for (int x = 0; x < rows; x++) {
			for (int y = 0; y < columns; y++) {
				int index = x * columns + y;
				if (index >= bitmaps.length)
					break;
				newBitmap = mixtureBitmap(newBitmap, bitmaps[index], new PointF(y * maxWidthPerImage, x * maxHeightPerImage));
			}
		}
		return newBitmap;
	}


/**
	 * Mix two Bitmap as one.
	 * 
	 * @param bitmapOne
	 * @param bitmapTwo
	 * @param point
	 *            where the second bitmap is painted.
	 * @return
	 */
	public static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) {
		if (first == null || second == null || fromPoint == null) {
			return null;
		}
		Bitmap newBitmap = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Config.ARGB_4444);
		Canvas cv = new Canvas(newBitmap);
		cv.drawBitmap(first, 0, 0, null);
		cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);
		cv.save(Canvas.ALL_SAVE_FLAG);
		cv.restore();
		return newBitmap;
	}


//截屏
public static Bitmap getScreenshotsForCurrentWindow(Activity activity) {
		View cv = activity.getWindow().getDecorView();
		Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_4444);
		cv.draw(new Canvas(bmp));
		return bmp;
	}


//旋转图片
// Rotates the bitmap by the specified degree.
// If a new bitmap is created, the original bitmap is recycled.
public static Bitmap rotate(Bitmap b, int degrees) {
	if (degrees != 0 && b != null) {
		Matrix m = new Matrix();
		m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
		try {
			b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
			 if (b != b2) {
			 b.recycle();
			 b = b2;
			 }
		} catch (OutOfMemoryError ex) {
			// We have no memory to rotate. Return the original bitmap.
		}
	}
		return b;
}


//可用于生成缩略图。
/**
 * Creates a centered bitmap of the desired size. Recycles the input.
 * 
 * @param source
 */
public static Bitmap extractMiniThumb(Bitmap source, int width, int height) {
	return extractMiniThumb(source, width, height, true);
}

public static Bitmap extractMiniThumb(Bitmap source, int width, int height, boolean recycle) {
	if (source == null) {
		return null;
	}

	float scale;
	if (source.getWidth() < source.getHeight()) {
		scale = width / (float) source.getWidth();
	} else {
		scale = height / (float) source.getHeight();
	}
	Matrix matrix = new Matrix();
	matrix.setScale(scale, scale);
	Bitmap miniThumbnail = transform(matrix, source, width, height, false);

	if (recycle && miniThumbnail != source) {
		source.recycle();
	}
	return miniThumbnail;
}

public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp) {
		int deltaX = source.getWidth() - targetWidth;
		int deltaY = source.getHeight() - targetHeight;
		if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
			/*
			 * In this case the bitmap is smaller, at least in one dimension,
			 * than the target. Transform it by placing as much of the image as
			 * possible into the target and leaving the top/bottom or left/right
			 * (or both) black.
			 */
			Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
			Canvas c = new Canvas(b2);

			int deltaXHalf = Math.max(0, deltaX / 2);
			int deltaYHalf = Math.max(0, deltaY / 2);
			Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf
					+ Math.min(targetHeight, source.getHeight()));
			int dstX = (targetWidth - src.width()) / 2;
			int dstY = (targetHeight - src.height()) / 2;
			Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
			c.drawBitmap(source, src, dst, null);
			return b2;
		}
		float bitmapWidthF = source.getWidth();
		float bitmapHeightF = source.getHeight();

		float bitmapAspect = bitmapWidthF / bitmapHeightF;
		float viewAspect = (float) targetWidth / targetHeight;

		if (bitmapAspect > viewAspect) {
			float scale = targetHeight / bitmapHeightF;
			if (scale < .9F || scale > 1F) {
				scaler.setScale(scale, scale);
			} else {
				scaler = null;
			}
		} else {
			float scale = targetWidth / bitmapWidthF;
			if (scale < .9F || scale > 1F) {
				scaler.setScale(scale, scale);
			} else {
				scaler = null;
			}
		}

		Bitmap b1;
		if (scaler != null) {
			// this is used for minithumb and crop, so we want to filter here.
			b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
		} else {
			b1 = source;
		}

		int dx1 = Math.max(0, b1.getWidth() - targetWidth);
		int dy1 = Math.max(0, b1.getHeight() - targetHeight);

		Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);

		if (b1 != source) {
			b1.recycle();
		}

		return b2;
	}


//图片剪切
public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
	int width = r.width();
	int height = r.height();

	Bitmap croppedImage = Bitmap.createBitmap(width, height, config);

	Canvas cvs = new Canvas(croppedImage);
	Rect dr = new Rect(0, 0, width, height);
	cvs.drawBitmap(mBitmap, r, dr, null);
	return croppedImage;
}


//从任一Drawable得到Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
	Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
	drawable.draw(canvas);
	return bitmap;
}


/**
	 * Save Bitmap to a file.保存图片到SD卡。
	 * 
	 * @param bitmap
	 * @param file
	 * @return error message if the saving is failed. null if the saving is
	 *         successful.
	 * @throws IOException
	 */
	public static void saveBitmapToFile(Bitmap bitmap, String _file) throws IOException {
		BufferedOutputStream os = null;
		try {
			File file = new File(_file);
			// String _filePath_file.replace(File.separatorChar +
			// file.getName(), "");
			int end = _file.lastIndexOf(File.separator);
			String _filePath = _file.substring(0, end);
			File filePath = new File(_filePath);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			file.createNewFile();
			os = new BufferedOutputStream(new FileOutputStream(file));
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					Log.e(TAG_ERROR, e.getMessage(), e);
				}
			}
		}
	}
分享到:
评论

相关推荐

    android获取图片尺寸的两种方式及bitmap的缩放操作

    我就废话不多说了,大家还是直接看代码吧~ //Uri.parse(file://+result.getImage... //方法一:通过uri把图片转化为bitmap的方法 Bitmap bitmap= BitmapFactory.decodeFile&#40;path&#41;; int height= bitmap.get

    Android bitmap工具类

    Android中bitmap转化成string格式的工具类,主要用于联网操作传递数据情境中

    Android开发、Bitmap 压缩相关操作、文件压缩、文件处理、图片处理、字符串处理等处理工具类

    Android开发、Bitmap 压缩相关操作(计算图片的压缩比率 计算图片的压缩比率 从Resources中加载图片 通过传入的bitmap,进行压缩,得到符合标准的bitmap 从SD卡上加载图片 删除临时图片)、文件压缩(压缩成文件 ...

    Android 图片Bitmap的剪切的示例代码

    一、什么是Android中的Bitmap Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。 二、Bitmap的剪切基本操作 代码如下:...

    Android开发之图片旋转功能实现方法【基于Matrix】

    本文实例讲述了Android开发之图片旋转功能实现方法。分享给大家供大家参考,具体如下: 在Android中进行图像旋转需要使用Matrix,它包含了一个3*3的矩阵,专门用于进行图像变换匹配。Matrix ,中文里叫矩阵,高等...

    Android 实现将Bitmap 保存到本地

    但是对于图片的操作也是比较的复杂。今天,我们学习一下如是将我们的图片保存到我们的本地。 开发环境 Android Studio 3.6 Android 11 Mac OS 10.15 模拟机 Google Pixel3 API R 然后学习一下如何来完成我们的功能 ...

    Android图片缓存之Bitmap详解(一)

    最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap、BitmapFactory这两个类。  Bitmap: Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取...

    Android中Bitmap用法实例分析

    主要介绍了Android中Bitmap用法,结合实例形式分析了Android操作图片的载入、属性设置、旋转等相关技巧,需要的朋友可以参考下

    Android开发中Bitmap高效加载使用详解

    在Android开发中,我们经常与Bitmap打交道,而对Bitmap的不恰当的操作经常会导致OOM(Out of Memory)。这篇文章我们会介绍如何高效地在Android开发中使用Bitmap,在保证图片显示质量的前提下尽可能占用更小的内存。

    Android中Bitmap常见的一些操作:缩放、裁剪、旋转和偏移

    Bitmap是Android中处理图片的一个重要的类,下面这篇文章主要给大家介绍了关于Android中Bitmap常见的一些操作:缩放、裁剪、旋转和偏移的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

    Android 缩放图片 缩小和放大图片.rar

    Android图片放大缩小操作范例,单击对应的按钮,可将一张图片进行放大或缩小显示,实现的步骤大概是:  取得屏幕分辨率大小 ,对获取到的屏幕高度扣除下方Button高度,定义缩小按钮onClickListener 和...

    Android性能优化之Bitmap图片优化详解

    在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitmap操作不慎,就容易造成OOM(Java.lang.OutofMemoryError – 内存溢出),本篇博客,我们将一起探讨Bitmap的性能优化。 为什么Bitmap会导致OOM? 1...

    Android App中使用Glide加载图片的教程

    试想一下,如果没有图片加载库,我们就要手动去下载图片,缓存图片,最后再从文件里面读取bitmap并设置到Imageview里面。这还算好的,要是在Listview里面你会更头疼的。原因我就不说了,你懂的~~再加上各种各样的...

    Android Bitmap的截取及状态栏的隐藏和显示功能

    Bitmap是Android系统中的图像处理中最重要类之一。Bitmap可以获取图像文件信息,对图像进行剪切、旋转、缩放,压缩等操作,并可以以指定格式保存图像文件。 正文如下: 最近项目中需要用到一个分享图片的功能,就...

    android中Bitmap用法(显示,保存,缩放,旋转)实例分析

    主要介绍了android中Bitmap用法,以实例形式较为详细的分析了android中Bitmap操作图片的显示、保存、缩放、旋转等相关技巧,需要的朋友可以参考下

    Android实现图像二值化代码

    Android实现图像二值化的代码,不知为什么灰度化时简单的像素操作不能达到合适的效果(二值化后图片发绿),最后用setSaturation(0)实现。本人刚学Android,代码是模仿其他人的一个反色处理写的。有不成熟的地方望...

    android调用系统拍照

    拍照或者从相册中选择图片后,我们都可以直接或间接的得到Uri或...大多数情况下操作图片不可避免的涉及到直接操作Bitmap,所以就需要解决图片旋转问题了,下面是演示如何拍照、选择相册中的图片后进行Bitmap旋转。

    android 如何从网络获取一张图片并显示

    首先应想到若要从网络资源中获取图片,就需要通过流操作,于是就想到如何创建流。 第一步:指定图片资源的URL 第二步:通过RUL获取一个connection 第三步:通过连接获取出入流 第四步:利用BitmapFactory....

Global site tag (gtag.js) - Google Analytics