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

本文介绍了Android系统相机在使用前置摄像头拍照时会进行镜像翻转,以达到真实视角效果,但因手机设计原因,仍存在差异。文章讨论了如何再次进行镜像操作,以修正这一问题,并通过Intent调用相机或图片应用进行演示。
部署运行你感兴趣的模型镜像

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.currentTimeMillis() + ".jpg");
			outFile.createNewFile();
			FileOutputStream outStream = new FileOutputStream(outFile);
			mBitmap.compress(CompressFormat.JPEG, 100, outStream);
			outStream.close();
		} catch (IOException e) {

		}
	}

上面的方法会将一个bitmap保存为jpg的图片文件。

拍照或者从本地选取图片

这里我们直接使用Intent来调用相机应用拍照或者图片应用来选择图片,如下

private void dispatchTakePictureIntent(int actionCode) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		try {
			File picFile = new File(Utils.getCameraFilePath(), System.currentTimeMillis() + ".jpg");
			picFile.createNewFile();
			mPhotoPath = picFile.getPath();
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile));
		} catch (IOException e) {
			e.printStackTrace();
		}
		startActivityForResult(intent, actionCode);
	}

	private void dispatchGetPictureIntent(int actionCode) {
		mPhotoPath = null;
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent, actionCode);
	}

在调用其它应用后我们需要得到操作的图片,所以使用startActivityForResult()方法,在onActivityResult()中得到图片路径,

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {

			// mPhotopath为空是指从相册选择的图片
			if (mPhotoPath == null) {
				Uri photoUri = data.getData();
				ContentResolver cr = this.getContentResolver();
				Cursor cursor = cr.query(photoUri, null, null, null, null);// 根据Uri从数据库中找
				if (cursor.moveToFirst()) {
					// 获取图片路径
					mPhotoPath = cursor.getString(cursor
							.getColumnIndex("_data"));
				}
				cursor.close();
			}

			if (mBitmap != null) {
				mBitmap.recycle();
				mBitmap = null;
			}
			mBitmap = BitmapFactory.decodeFile(mPhotoPath);
			if (mBitmap != null) {
				Bitmap temp = convertBmp(mBitmap);
				if (temp != null) {
					mImageView.setImageBitmap(temp);
					mBitmap.recycle();
					mBitmap = temp;
				}
			}
			mImageView.setImageBitmap(mBitmap);

		}

因为从相册选取本地图片后intent中只会保留一个缩略图,因此我们需要去查询媒体库来得到图片的具体路径。

下面是实际的运行效果



您可能感兴趣的与本文相关的镜像

GPT-SoVITS

GPT-SoVITS

AI应用

GPT-SoVITS 是一个开源的文本到语音(TTS)和语音转换模型,它结合了 GPT 的生成能力和 SoVITS 的语音转换技术。该项目以其强大的声音克隆能力而闻名,仅需少量语音样本(如5秒)即可实现高质量的即时语音合成,也可通过更长的音频(如1分钟)进行微调以获得更逼真的效果

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值