日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android crop 大图,com.android.camera.action.CROP 实现图片剪裁

發布時間:2023/12/9 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android crop 大图,com.android.camera.action.CROP 实现图片剪裁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

APP 中選取圖片之后,有時候需要進行剪裁,比如頭像。

以下是啟動代碼。

在我的項目中,傳的是 filePath,所以我轉了一下,但實際上從相冊選擇圖片后,用 data.getData() 就可獲得 uri。Uri uri = Uri.fromFile(new File(filePath));

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

intent.setDataAndType(, "image/*");

intent.putExtra("crop", "true"); // 可剪裁

intent.putExtra("aspectX", 10); // 高比例

intent.putExtra("aspectY", 10); // 寬比例

intent.putExtra("outputX", size); // 寬尺寸

intent.putExtra("outputY", size); // 高尺寸

intent.putExtra("scale", true); // 保持比例

intent.putExtra("return-data", true); // 剪裁后,是否返回 Bitmap

intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // 輸出 JPEG

intent.putExtra("noFaceDetection", false); // 人臉識別,開啟后,探測到人臉后會將剪裁框移到人臉上

activity.startActivityForResult(intent, 123456); // 啟動

以下是剪裁后的處理:@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 123456 && resultCode == RESULT_OK) {

// 用戶頭像剪裁后

Bitmap bitmap = data.getExtras().getParcelable("data");

// 演示保存到緩存中

// 目錄

String cacheDirectoryPath = getCacheDir().getPath();

File dir = new File(cacheDirectoryPath);

if (!dir.exists()) {

dir.mkdirs(); // 目錄不存在就創建

}

// 文件保存

String filePath = cacheDirectoryPath + File.separator + "cutted.jpg";

File file = new File(filePath);

try {

FileOutputStream fos = new FileOutputStream(file);

//通過io流的方式來壓縮保存圖片

bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);

fos.flush();

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

注意:由于安卓的亂象,啟動剪裁時,某些手機中會報 ActivityNotFound 的異常。

總結

以上是生活随笔為你收集整理的android crop 大图,com.android.camera.action.CROP 实现图片剪裁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。