android實(shí)現(xiàn)圖片裁剪的兩種方法
兩種android圖片裁剪方式,供大家參考,具體內(nèi)容如下
一、相機(jī)拍完照之后利用系統(tǒng)自帶裁剪工具進(jìn)行截取
public static void cropImage(Activity activity, Uri srcUri) {
cropImageUri = srcUri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(srcUri, "image/*");
intent.putExtra("crop", "true");
// 1.寬高和比例都不設(shè)置時,裁剪框可以自行調(diào)整(比例和大小都可以隨意調(diào)整)
////////////////////////////////////////////////////////////////
// 2.只設(shè)置裁剪框?qū)捀弑?aspect)后,裁剪框比例固定不可調(diào)整,只能調(diào)整大小
////////////////////////////////////////////////////////////////
// 3.裁剪后生成圖片寬高(output)的設(shè)置和裁剪框無關(guān),只決定最終生成圖片大小
////////////////////////////////////////////////////////////////
// 4.裁剪框?qū)捀弑壤?aspect)可以和裁剪后生成圖片比例(output)不同,此時,
// 會以裁剪框的寬為準(zhǔn),按照裁剪寬高比例生成一個圖片,該圖和框選部分可能不同,
// 不同的情況可能是截取框選的一部分,也可能超出框選部分,向下延伸補(bǔ)足
////////////////////////////////////////////////////////////////
// aspectX aspectY 是裁剪框?qū)捀叩谋壤?
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
// // outputX outputY 是裁剪后生成圖片的寬高
// intent.putExtra("outputX", 300);
// intent.putExtra("outputY", 300);
// return-data為true時,會直接返回bitmap數(shù)據(jù),但是大圖裁剪時會出現(xiàn)問題,推薦下面為false時的方式
// return-data為false時,不會返回bitmap,但需要指定一個MediaStore.EXTRA_OUTPUT保存圖片uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
intent.putExtra("return-data", false);
activity.startActivityForResult(intent, CROP_IMAGE);
}
這樣圖片可以通過手勢來自由裁剪
二、自定義相機(jī)拍照界面,裁剪固定區(qū)域的圖片
這里用到了一個叫idcardcamera的三方,在此向原作者致敬,附上項(xiàng)目引用地址 ‘com.github.wildma:IDCardCamera:1.0.0'
有幾個地方,需要修改一下,我是在相機(jī)中間加了一個長方形的框,代碼片段如下:
float screenMinSize = Math.min(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
//根據(jù)screenMinSize,計(jì)算出cameraPreview的較寬的一邊,長寬比為標(biāo)準(zhǔn)的16:9
float maxSize = screenMinSize / 9.0f * 16.0f;
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) maxSize, (int) screenMinSize);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mCameraPreview.setLayoutParams(layoutParams);
float height = (int) (screenMinSize * 0.3);
float density = getResources().getDisplayMetrics().density; // 屏幕密度(0.75 / 1.0 / 1.5)
float mywdith =(int) 136*density;
int widthPixels = getResources().getDisplayMetrics().widthPixels;
float width = (int) (widthPixels-mywdith);//75 47
LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams((int) width, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams cropParams = new LinearLayout.LayoutParams((int) width, (int) height);
mLlCameraCropContainer.setLayoutParams(containerParams);
mIvCameraCrop.setLayoutParams(cropParams);
修改了 mIvCameraCrop的大小,給這個長方形框添加一個背景邊框,剛開始老是顯示不出來右邊的邊框,后來發(fā)現(xiàn),拍照按鈕父布局的寬度為136dp,用代碼計(jì)算出實(shí)際占用的px,然后設(shè)置長方形的合適寬度,這樣就遮擋不住了,右邊的邊框就能夠顯示出來了,附上拍照的邏輯,選擇自動剪裁
/**
* 拍照
*/
private void takePhoto() {
mCameraPreview.setEnabled(false);
mCameraPreview.takePhoto(new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
// camera.stopPreview();
camera.startPreview();
//子線程處理圖片,防止ANR
// setCropLayout();
new Thread(new Runnable() {
@Override
public void run() {
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
String imagePath = path;
ImageUtils.save(bitmap, imagePath, Bitmap.CompressFormat.JPEG);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int angle = ImageUtils.getBitmapDegree(imagePath);
if (mOrientation==90){
bitmap = ImageUtils.rotateBitmap(CameraActivity.this.bitmap, 180);
}
/**
* 計(jì)算裁剪位置
*/
float left, top, right, bottom;
left = ((float) mLlCameraCropContainer.getLeft() - (float) mCameraPreview.getLeft()) / (float) mCameraPreview.getWidth();
top = (float) mIvCameraCrop.getTop() / (float) mCameraPreview.getHeight();
right = (float) mLlCameraCropContainer.getRight() / (float) mCameraPreview.getWidth();
bottom = (float) mIvCameraCrop.getBottom() / (float) mCameraPreview.getHeight();
/**
* 自動裁剪
**/
mCropBitmap = Bitmap.createBitmap(bitmap,
(int) (left * (float) bitmap.getWidth()),
(int) (top * (float) bitmap.getHeight()),
(int) ((right - left) * (float) bitmap.getWidth()),
(int) ((bottom - top) * (float) bitmap.getHeight()));
/**
* 手動裁剪
**/
runOnUiThread(new Runnable() {
@Override
public void run() {
//將裁剪區(qū)域設(shè)置成與掃描框一樣大
setCropLayout();
mCropImageView.setLayoutParams(new LinearLayout.LayoutParams(mIvCameraCrop.getWidth(), mIvCameraCrop.getHeight()));
mCropImageView.setImageBitmap( mCropBitmap);
}
});
}
}).start();
safeToTakePicture = true;
}
});
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法
- Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android圖片裁剪功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)相機(jī)拍攝、選擇、圖片裁剪功能
- Android開發(fā)從相機(jī)或相冊獲取圖片裁剪
- 使用Java代碼在Android中實(shí)現(xiàn)圖片裁剪功能
相關(guān)文章
android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Android 實(shí)現(xiàn)全屏和無標(biāo)題欄的顯示
本篇文章主要介紹了Android 全屏顯示和無標(biāo)題欄的方法,并附上代碼實(shí)例,和運(yùn)行效果圖,有需要的朋友可以參考下2016-07-07
Android編程實(shí)現(xiàn)ListView滾動提示等待框功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)ListView滾動提示等待框功能,結(jié)合實(shí)例形式分析了Android ListView滾動事件相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
手把手教你實(shí)現(xiàn)Android編譯期注解
今天給大家介紹Android編譯期注解sdk的步驟以及注意事項(xiàng),并簡要分析了運(yùn)行時注解以及字節(jié)碼技術(shù)在生成代碼上與編譯期注解的不同與優(yōu)劣,感興趣的朋友一起看看吧2021-07-07
Android4.1中BinderService用法實(shí)例分析
這篇文章主要介紹了Android4.1中BinderService用法,以實(shí)例形式分析了Android4.1新增BinderService類的功能、原理及使用技巧,具有一定參考借鑒價值2015-10-10
Android 滑動小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程
Viewpager,視圖翻頁工具,提供了多頁面切換的效果。Android 3.0后引入的一個UI控件,位于v4包中。低版本使用需要導(dǎo)入v4包,現(xiàn)在我們一般不再兼容3.0及以下版本,另外使用Android studio開發(fā),默認(rèn)導(dǎo)入v7包,v7包含了v4,所以不用導(dǎo)包,越來越方便了2021-11-11
Android HTTP網(wǎng)絡(luò)請求的異步實(shí)現(xiàn)
這篇文章主要介紹了Android HTTP網(wǎng)絡(luò)請求的異步實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-07-07

