您好,登錄后才能下訂單哦!
Android項目中如何實現自定義相機預覽界面?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
官方文檔:
public static void setCameraDisplayOrientation(Activity activity,int cameraIo, Camera camera){ Camera.CameraInfo info=new Camera.CameraInfo(); Camera.getCameraInfo(cameraIo,info); int rotation=activity.getWindowManager().getDefaultDisplay().getRotation(); int degress=0; switch(rotation){ case Surface.ROTATION_0: degress=0; break; case Surface.ROTATION_90: degress=90; break; case Surface.ROTATION_180: degress=180; break; case Surface.ROTATION_270: degress=270; break; } int result; if(info.facing=Camera.CameraInfo.CAMERA_FACING_FRONT){ result = (info.orientation + degrees) % 360; > 就是攝像頭需要順時針轉過多少度才能恢復自然方向 result = (360 - result) % 360; } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result); switch (result) { case 0: case 180: setCameraSize(camera.getParameters(), getScreenWidth(), getScreenHeight()); break; case 90: case 270: setCameraSize(camera.getParameters(), getScreenHeight(), getScreenWidth()); break; } } public static void setCameraSize(Camera.Parameters parameters, int width, int height) { Map<String, List<Size>> allSizes = new HashMap<>(); String typePreview = "typePreview"; String typePicture = "typePicture"; allSizes.put(typePreview, parameters.getSupportedPreviewSizes()); allSizes.put(typePicture, parameters.getSupportedPictureSizes()); Iterator iterator = allSizes.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, List<Size>> entry = (Map.Entry<String, List<Size>>) iterator.next(); List<Size> sizes = entry.getValue(); if (sizes == null || sizes.isEmpty()) continue; ArrayList<WrapCameraSize> wrapCameraSizes = new ArrayList<>(sizes.size()); for (Size size : sizes) { WrapCameraSize wrapCameraSize = new WrapCameraSize(); wrapCameraSize.setWidth(size.width); wrapCameraSize.setHeight(size.height); wrapCameraSize.setD(Math.abs((size.width - width)) + Math.abs((size.height - height))); if (size.width == width && size.height == height) { if (typePreview.equals(entry.getKey())) { parameters.setPreviewSize(size.width, size.height); } else if (typePicture.equals(entry.getKey())) { parameters.setPictureSize(size.width, size.height); } Log.d(TAG, "best size: width=" + size.width + ";height=" + size.height); break; } wrapCameraSizes.add(wrapCameraSize); } Log.d(TAG, "wrapCameraSizes.size()=" + wrapCameraSizes.size()); Size resultSize = null; if (typePreview.equals(entry.getKey())) { resultSize = parameters.getPreviewSize(); } else if (typePicture.equals(entry.getKey())) { resultSize = parameters.getPictureSize(); } if (resultSize == null || (resultSize.width != width && resultSize.height != height)) { //找到相機Preview Size 和 Picture Size中最適合的大小 if(wrapCameraSizes.isEmpty()) continue; WrapCameraSize minCameraSize = Collections.min(wrapCameraSizes); while (!(minCameraSize.getWidth() >= width && minCameraSize.getHeight() >= height)) { wrapCameraSizes.remove(minCameraSize); if(wrapCameraSizes.isEmpty()) break; minCameraSize = null; minCameraSize = Collections.min(wrapCameraSizes); } Log.d(TAG, "best min size: width=" + minCameraSize.getWidth() + ";height=" + minCameraSize.getHeight()); if (typePreview.equals(entry.getKey())) { parameters.setPreviewSize(minCameraSize.getWidth(), minCameraSize.getHeight()); } else if (typePicture.equals(entry.getKey())) { parameters.setPictureSize(minCameraSize.getWidth(), minCameraSize.getHeight()); } } iterator.remove(); } }
先將獲取手機支持預覽的尺寸列表通過parmeters.getSupportPreviewSize返回的是一個集合。
進行屏幕方向的判斷,因為預覽的尺寸都是w>h 如果是豎屏,則需要將寬和高進行調換。
將預覽尺寸列表的每個元素的寬和高與SurfaceView的寬和高進行比較,如果存在寬和高尺寸SurfaceView的寬和高,相同的size,則將當前的寬高設置為預覽尺寸。
如果沒有找到該步驟,則將尺寸列表的比例和SUrfaceView的比例做比較,找一個相同或者相近的 。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。