拍照与上传

本文详细介绍了如何在Android应用中使用拍照功能,并通过指定路径保存照片,然后进行裁剪操作,包括设置裁剪的比例、宽高等参数,最终获取裁剪后的照片。此外,还演示了如何从相册选择图片并进行裁剪。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//拍照


Intent cIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
cIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(cIntent, Constant.PHOTO_REQUEST_TAKEPHOTO);

//裁剪-----------------------------------------------------

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");


// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 3);


// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);


// 添加
intent.putExtra("scale", false);

startActivityForResult(intent, Constant.PHOTO_REQUEST_CUT);

//-------------------------------------------------------------

                 if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");

}
}



//选图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 2);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 200);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, Constant.CHOOSE_SMALL_PICTURE);




//Bitmap转化为Base64

private String encodeToBaseByte(Bitmap bitmap) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
byte[] buffer = out.toByteArray();
byte[] encode = Base64.encode(buffer, Base64.DEFAULT);
return new String(encode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值