Android自拍相机应用——图片的镜像翻转

本文介绍了Android系统相机在使用前置摄像头拍照时会进行镜像翻转,以达到真实视角效果,但因手机设计原因,仍存在差异。文章讨论了如何再次进行镜像操作,以修正这一问题,并通过Intent调用相机或图片应用进行演示。
摘要由CSDN通过智能技术生成

Android系统相机在使用前置摄像头拍照的时候,最终会将拍下的画面做镜像翻转,来达到真实的视角效果,但由于Android手机大多将前置摄像头摆在左右两边(iPhone在靠中间的位置,效果会好很多),导致翻转后与拍照预览的画面还是会有较大的差别,所以我们这里就再做一次镜像将图片翻转回去。

图片镜像

public Bitmap convertBmp(Bitmap bmp) {
		int w = bmp.getWidth();
		int h = bmp.getHeight();

		Matrix matrix = new Matrix();
		matrix.postScale(-1, 1); // 镜像水平翻转
		Bitmap convertBmp = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, true);
		
		return convertBmp;
	}

图片的翻转主要用到变换矩阵中的缩放操作,x轴都乘-1,y轴保持不变。

图片保存

	private void saveBitmap() {
		try {
			File outFile = new File(Utils.getRootPath(),
					System.c
在Java中实现图片镜像翻转可以通过使用Graphics2D类的方法来实现。首先,我们需要创建一个BufferedImage对象来装载原始图片,并且获取其宽度和高度,然后创建一个新的BufferedImage对象作为目标图片。接着,我们可以通过Graphics2D对象的drawImage()方法将原始图片绘制到目标图片上,并通过设置AffineTransform对象的scale()方法来进行水平或垂直方向的镜像翻转。最后,将目标图片保存到磁盘上或者显示在界面上。 具体地,我们可以通过如下的代码来实现图片的水平镜像翻转: ```java public BufferedImage flipImageHorizontally(BufferedImage originalImage) { int width = originalImage.getWidth(); int height = originalImage.getHeight(); BufferedImage flippedImage = new BufferedImage(width, height, originalImage.getType()); Graphics2D g = flippedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, width, 0, 0, height, null); g.dispose(); return flippedImage; } ``` 而如果需要实现垂直镜像翻转,可以通过如下的代码来完成: ```java public BufferedImage flipImageVertically(BufferedImage originalImage) { int width = originalImage.getWidth(); int height = originalImage.getHeight(); BufferedImage flippedImage = new BufferedImage(width, height, originalImage.getType()); Graphics2D g = flippedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, 0, height, width, 0, null); g.dispose(); return flippedImage; } ``` 以上就是在Java中实现图片镜像翻转的简单方法,通过Graphics2D类和AffineTransform对象的配合,可以轻松实现图片的水平或垂直镜像翻转
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值