網站首頁 編程語言 正文
本文實例為大家分享了Android自定義相機,預覽區域裁剪的具體代碼,供大家參考,具體內容如下
寫法一:
預覽區域裁剪,方法調用:
//按照比例進行裁剪頭像區域
Bitmap ? resultBitmap = getScaleImage(resultBitmap,
?(int) cuttingAreaView.getX(),
? (int) cuttingAreaView.getY(),?
? cuttingAreaView.getWidth(),?
? cuttingAreaView.getHeight(),?
? mSurfaceView.getWidth(),?
? mSurfaceView.getHeight());
/**
? ? ?* 按照比例裁剪圖片
? ? ?*
? ? ?* @param source
? ? ?* @param cuttingAreaX ?預覽view的X坐標
? ? ?* @param cuttingAreaY
? ? ?* @param cuttingAreaWidth
? ? ?* @param cuttingAreaHeight
? ? ?* @param displayWidth
? ? ?* @param displayHeight
? ? ?* @return
? ? ?*/
? ? private Bitmap getScaleImage(Bitmap source, int cuttingAreaX, int cuttingAreaY, int cuttingAreaWidth, int cuttingAreaHeight, int displayWidth, int displayHeight) {
? ? ? ? int sourceWidth = source.getWidth();
? ? ? ? int sourceHeight = source.getHeight();
? ? ? ? LegoLog.d("sourceWidth:" + sourceWidth + ",sourceHeight:" + sourceHeight + ",cuttingAreaX:" + cuttingAreaX + ",cuttingAreaY:" + cuttingAreaY + ",cuttingAreaWidth:" + cuttingAreaWidth + ",cuttingAreaHeight:" + cuttingAreaHeight + ",displayWidth:" + displayWidth + ",displayHeight:" + displayHeight);
? ? ? ? int sourceCuttingAreaX = cuttingAreaX * sourceWidth / displayWidth;
? ? ? ? int sourceCuttingAreaY = cuttingAreaY * sourceHeight / displayHeight;
? ? ? ? int sourceCuttingAreaWidth = cuttingAreaWidth * sourceWidth / displayWidth;
? ? ? ? int sourceCuttingAreaHeight = cuttingAreaHeight * sourceHeight / displayHeight;
? ? ? ? LegoLog.d("sourceWidth:" + sourceWidth + ",sourceHeight:" + sourceHeight + ",sourceCuttingAreaX:" + sourceCuttingAreaX + ",sourceCuttingAreaY:" + sourceCuttingAreaY + ",sourceCuttingAreaWidth:" + sourceCuttingAreaWidth + ",sourceCuttingAreaHeight:" + sourceCuttingAreaHeight);
? ? ? ? return Bitmap.createBitmap(source, sourceCuttingAreaX, sourceCuttingAreaY, sourceCuttingAreaWidth, sourceCuttingAreaHeight, null, false);
? ? }
其他方法:
private void initParameters(Camera camera) {
? ? ? ? try {
? ? ? ? ? ? mParameters = camera.getParameters();
? ? ? ? ? ? mParameters.setPreviewFormat(ImageFormat.NV21);
? ? ? ? ? ? //獲取與指定寬高相等或最接近的尺寸
? ? ? ? ? ? //設置預覽尺寸
? ? ? ? ? ? Camera.Size bestPreviewSize = getBestSize(mSurfaceView.getWidth(), mSurfaceView.getHeight(), mParameters.getSupportedPreviewSizes());
? ? ? ? ? ? if (bestPreviewSize != null) {
? ? ? ? ? ? ? ? mParameters.setPreviewSize(bestPreviewSize.width, bestPreviewSize.height);
? ? ? ? ? ? }
? ? ? ? ? ? //設置保存圖片尺寸
? ? ? ? ? ? Camera.Size bestPicSize = getBestSize(PIC_WIDTH, PIC_HEIGHT, mParameters.getSupportedPictureSizes());
? ? ? ? ? ? if (bestPicSize != null) {
? ? ? ? ? ? ? ? mParameters.setPictureSize(bestPicSize.width, bestPicSize.height);
? ? ? ? ? ? }
? ? ? ? ? ? //對焦模式
? ? ? ? ? ? if (isSupportFocus(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
? ? ? ? ? ? ? ? mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
? ? ? ? ? ? }
? ? ? ? ? ? camera.setParameters(mParameters);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ??
? ? private Camera.Size getBestSize(int targetWidth, int targetHeight, List<Camera.Size> sizeList) {
? ? ? ? Camera.Size bestSize = null;
? ? ? ? float targetRatio = ((float) targetHeight / targetWidth); ?//目標大小的寬高比
? ? ? ? float minDiff = targetRatio;
? ? ? ? for (Camera.Size size : sizeList) {
? ? ? ? ? ? if (size.width == targetHeight && size.height == targetWidth) {
? ? ? ? ? ? ? ? bestSize = size;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? float supportedRatio = (float) size.width / size.height;
? ? ? ? ? ? if (Math.abs(supportedRatio - targetRatio) < minDiff) {
? ? ? ? ? ? ? ? minDiff = Math.abs(supportedRatio - targetRatio);
? ? ? ? ? ? ? ? bestSize = size;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return bestSize;
? ? }
參考【人車核驗】CaptureManager.java
寫法二:
Bitmap bitmap = BitmapFactory.decodeFile(originalFile.getPath());//原圖
//計算裁剪位置
float left, top, right, bottom;
left = (float) scanView.getLeft() / (float) cameraPreview.getWidth();
top = ((float) containerView.getTop() - (float) cameraPreview.getTop()) / (float) cameraPreview.getHeight();
right = (float) scanView.getRight() / (float) cameraPreview.getWidth();
bottom = (float) containerView.getBottom() / (float) cameraPreview.getHeight();
//裁剪及保存到文件
Bitmap cropBitmap = 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()));
參考:MobileCheck
原文鏈接:https://blog.csdn.net/zhijiandedaima/article/details/122324604
相關推薦
- 2022-06-19 C++詳細分析講解函數參數的擴展_C 語言
- 2023-07-15 react實現路由懶加載
- 2022-06-22 Android使用EventBus多次接收消息_Android
- 2022-01-21 面試題:說一說es6新增方法
- 2023-12-16 IDEA中設置遠程調試服務器上的程序
- 2022-07-08 ???????C語言實現單鏈表基本操作方法_C 語言
- 2022-05-11 C++類繼承時的構造函數_C 語言
- 2022-04-23 npm publish 組件流程以及報錯總結
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支