Android自定義相機(jī)、預(yù)覽區(qū)域裁剪
本文實(shí)例為大家分享了Android自定義相機(jī),預(yù)覽區(qū)域裁剪的具體代碼,供大家參考,具體內(nèi)容如下
寫法一:

預(yù)覽區(qū)域裁剪,方法調(diào)用:
//按照比例進(jìn)行裁剪頭像區(qū)域 Bitmap ? resultBitmap = getScaleImage(resultBitmap, ?(int) cuttingAreaView.getX(), ? (int) cuttingAreaView.getY(),? ? cuttingAreaView.getWidth(),? ? cuttingAreaView.getHeight(),? ? mSurfaceView.getWidth(),? ? mSurfaceView.getHeight());
/**
? ? ?* 按照比例裁剪圖片
? ? ?*
? ? ?* @param source
? ? ?* @param cuttingAreaX ?預(yù)覽view的X坐標(biāo)
? ? ?* @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);
? ? ? ? ? ? //獲取與指定寬高相等或最接近的尺寸
? ? ? ? ? ? //設(shè)置預(yù)覽尺寸
? ? ? ? ? ? Camera.Size bestPreviewSize = getBestSize(mSurfaceView.getWidth(), mSurfaceView.getHeight(), mParameters.getSupportedPreviewSizes());
? ? ? ? ? ? if (bestPreviewSize != null) {
? ? ? ? ? ? ? ? mParameters.setPreviewSize(bestPreviewSize.width, bestPreviewSize.height);
? ? ? ? ? ? }
? ? ? ? ? ? //設(shè)置保存圖片尺寸
? ? ? ? ? ? Camera.Size bestPicSize = getBestSize(PIC_WIDTH, PIC_HEIGHT, mParameters.getSupportedPictureSizes());
? ? ? ? ? ? if (bestPicSize != null) {
? ? ? ? ? ? ? ? mParameters.setPictureSize(bestPicSize.width, bestPicSize.height);
? ? ? ? ? ? }
? ? ? ? ? ? //對(duì)焦模式
? ? ? ? ? ? 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); ?//目標(biāo)大小的寬高比
? ? ? ? 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;
? ? }參考【人車核驗(yàn)】CaptureManager.java
寫法二:
Bitmap bitmap = BitmapFactory.decodeFile(originalFile.getPath());//原圖 //計(jì)算裁剪位置 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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio綁定下拉框數(shù)據(jù)詳解
這篇文章主要為大家詳細(xì)介紹了Android Studio綁定下拉框數(shù)據(jù),Android Studio綁定網(wǎng)絡(luò)JSON數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
android底部彈出iOS7風(fēng)格對(duì)話選項(xiàng)框(QQ對(duì)話框)--第三方開源之IOS_Dialog_Library
這篇文章主要介紹了android底部彈出iOS7風(fēng)格對(duì)話選項(xiàng)框(QQ對(duì)話框)--第三方開源--IOS_Dialog_Library的相關(guān)資料,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)應(yīng)用程序的閃屏效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)應(yīng)用程序的閃屏效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android 后臺(tái)調(diào)度任務(wù)與省電詳解
本文主要介紹 Android 后臺(tái)調(diào)度任務(wù)與省電,這里整理了詳細(xì)的知識(shí)資料供大家學(xué)習(xí)參考,希望能幫助有需要的小伙伴2016-08-08
android 微信 sdk api調(diào)用不成功解決方案
最近一直在調(diào)用微信的API,卻發(fā)現(xiàn)一直調(diào)用不成功,糾結(jié)了好久,各方面找教程,現(xiàn)在曬出來和大家分享一下2012-11-11
Android數(shù)據(jù)加密之Rsa加密的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狝ndroid數(shù)據(jù)加密之Rsa加密的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
基于Manifest.xml中不要出現(xiàn)重復(fù)的uses permission的說明
本篇文章對(duì)Manifest.xml中不要出現(xiàn)重復(fù)的uses permission進(jìn)行了介紹。需要的朋友參考下2013-05-05

