1. 程式人生 > >自己寫的照相機預覽(TextureView)

自己寫的照相機預覽(TextureView)

android:hardwareAccelerated="true"

還是照舊貼上程式碼 為了簡單起見我遮蔽掉了其他功能,精簡出了預覽版本。


import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import
android.view.TextureView; import com.imapedia.ykdemo.utils.L; import java.io.IOException; import java.util.List; /** * Created by ning_gg on 2017/1/6. */ public class CameraTextureView extends TextureView implements TextureView.SurfaceTextureListener{ private String TAG=CameraTextureView.class.getSimpleName(); private
Context mContext; private Camera mCamera; private int screenHeight;//螢幕的高度 private int screenWidth;//螢幕的寬度 /*** * 是否支援自動對焦 */ private boolean isSupportAutoFocus; public static Camera.Size pictureSize; private Camera.Size previewSize; public CameraTextureView(Context context,AttributeSet attrs) { super
(context,attrs); init(context); this.setSurfaceTextureListener(this); } private void init(Context context) { mContext = context; DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; isSupportAutoFocus = context.getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_AUTOFOCUS); } @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { L.e(TAG,"onSurfaceTextureAvailable"); try { mCamera = Camera.open(); if (mCamera == null) { return; } mCamera.setDisplayOrientation(90); // 設定holder主要是用於surfaceView的圖片的實時預覽,以及獲取圖片等功能,可以理解為控制camera的操作.. mCamera.setPreviewTexture(surfaceTexture); setCameraParms(); //mCamera.setPreviewCallback(this); mCamera.startPreview(); mCamera.cancelAutoFocus(); requestLayout(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { L.e(TAG,"onSurfaceTextureSizeChanged"); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { L.e(TAG,"onSurfaceTextureDestroyed"); if (mCamera != null) { mCamera.setPreviewCallback(null); mCamera.stopPreview(); mCamera.release(); mCamera = null; } return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { L.e(TAG,"onSurfaceTextureUpdated"); } private void setCameraParms() { Camera.Parameters myParam = mCamera.getParameters(); List<String> flashModes = myParam.getSupportedFlashModes(); String flashMode = myParam.getFlashMode(); // Check if camera flash exists if (flashModes == null) { return; } if (!Camera.Parameters.FLASH_MODE_OFF.equals(flashMode)) { // Turn off the flash if (flashModes.contains(Camera.Parameters.FLASH_MODE_OFF)) { myParam.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); } else { } } float percent = calcPreviewPercent(); List<Camera.Size> supportedPreviewSizes = myParam.getSupportedPreviewSizes(); previewSize = getPreviewMaxSize(supportedPreviewSizes, percent); L.e(TAG, "預覽尺寸w===" + previewSize.width + ",h===" + previewSize.height); // 獲取攝像頭支援的各種解析度 List<Camera.Size> supportedPictureSizes = myParam.getSupportedPictureSizes(); pictureSize = findSizeFromList(supportedPictureSizes, previewSize); if (pictureSize == null) { pictureSize = getPictureMaxSize(supportedPictureSizes, previewSize); } L.e(TAG, "照片尺寸w===" + pictureSize.width + ",h===" + pictureSize.height); // 設定照片解析度,注意要在攝像頭支援的範圍內選擇 myParam.setPictureSize(pictureSize.width, pictureSize.height); // 設定預瀏尺寸,注意要在攝像頭支援的範圍內選擇 myParam.setPreviewSize(previewSize.width, previewSize.height); myParam.setJpegQuality(70); mCamera.setParameters(myParam); } private float calcPreviewPercent() { float d = screenHeight; return d / screenWidth; } private Camera.Size findSizeFromList(List<Camera.Size> supportedPictureSizes, Camera.Size size) { Camera.Size s = null; if (supportedPictureSizes != null && !supportedPictureSizes.isEmpty()) { for (Camera.Size su : supportedPictureSizes) { if (size.width == su.width && size.height == su.height) { s = su; break; } } } return s; } // 根據攝像頭的獲取與螢幕解析度最為接近的一個解析度 private Camera.Size getPictureMaxSize(List<Camera.Size> l, Camera.Size size) { Camera.Size s = null; for (int i = 0; i < l.size(); i++) { if (l.get(i).width >= size.width && l.get(i).height >= size.width && l.get(i).height != l.get(i).width) { if (s == null) { s = l.get(i); } else { if (s.height * s.width > l.get(i).width * l.get(i).height) { s = l.get(i); } } } } return s; } // 獲取預覽的最大解析度 private Camera.Size getPreviewMaxSize(List<Camera.Size> l, float j) { int idx_best = 0; int best_width = 0; float best_diff = 100.0f; for (int i = 0; i < l.size(); i++) { int w = l.get(i).width; int h = l.get(i).height; if (w * h < screenHeight * screenWidth) continue; float previewPercent = (float) w / h; float diff = Math.abs(previewPercent - j); if (diff < best_diff) { idx_best = i; best_diff = diff; best_width = w; } else if (diff == best_diff && w > best_width) { idx_best = i; best_diff = diff; best_width = w; } } return l.get(idx_best); } }

Camera有一組預覽尺寸,也有一組錄影尺寸,還有一組照片尺寸,有人說的錄影時尺寸會變形那是因為預覽跟錄影不搭配的問題導致的,我也做了這個修改,最起碼我自己的多個測試手機沒有這種變形的問題了。過幾天再貼這個錄影的吧。

相關推薦

自己寫的照相機TextureView

android:hardwareAccelerated="true" 還是照舊貼上程式碼 為了簡單起見我遮蔽掉了其他功能,精簡出了預覽版本。 import android.content.Context; import android.cont

上傳shp檔案到geoserver伺服器並2

1.1、配置資料來源 geoserver官方中使用的資料nyc_roads.zip 至此說明上傳到伺服器成功, 1

fastDFS+LibreOffice檔案:轉為pdf格式線上

注意: 1)需要下載LibreOffice 2)括號裡的LibreOffice是這個程式的安裝位置 // 連線OpenOffice/LibreOffice服務 此地址為服務端安裝專案路徑,本地需要自行安裝libreoffice OfficeManager offic

微信小程式圖片選擇、上傳到伺服器、PHP實現例項

微信小程式圖片選擇、上傳到伺服器、預覽(PHP)實現例項 小程式實現選擇圖片、預覽圖片、上傳到開發者伺服器上 後臺使用的tp3.2 圖片上傳  請求時候的header參考時可以去掉(個人後臺驗證許可權使用) 小程式前端程式碼: <view class="se

Bootstrap多圖片上傳並tp5

Bootstrap多圖上傳版本1.0(1)非同步上傳檔案並儲存到資料庫(2)限制檔案上傳個數(3)監聽檔案是否上傳成功一、控制器//多圖上傳 public function imgupload() {

【選擇圖片Jar】全屏DialogFragment實現圖片詳情

1.前言 上一篇文章我們在最後丟擲了一個問題。到底我們要如何實現圖片的詳情頁的預覽。 如圖: 相信大家看了標題已經知道了,我們是用DialogFragment實現我們這個功能的! 嘿嘿,可能這個時候會有點驚訝,原來DialogFragment還有這樣的

圖片上傳可根據自己得需要封裝元件

話不多說,先上圖   實現簡單得多行多圖片上傳,可以選擇其中任意一個圖片當作你需要得預設圖傳到後臺,然後一次性提交 圖片上傳重要得也就兩個屬性:new FormData()和new FileReader() 程式碼: <template> <

Android camera2 實現相機及獲取幀資料流

一、本文重點說明 本文基於 android camera2 實現視訊預覽,暫未相容 camera1 API,基礎實現可以參考 googlesample Camera2 例子 android-Camera2Basic ,本文以工具類形式實現一步呼叫。 谷歌例子中沒有具體指

MUI之圖片zoom.js和previewimage.js

line fill dex size posit fixed slide col auto 1 <style type="text/css"> 2 .mui-preview-image.mui-fullscreen { 3

簡單的做一個圖片上傳web前端

chrom 預覽 web前端 console fine 分享圖片 fire title right 轉載:點擊查看原文 在做web項目很多的時候圖片都是避免不了的,所以操作圖片就成了一個相對比較棘手的問題,其實也不是說很麻煩,只是說上傳然後直接預覽的過

java web通過openoffice實現文件網頁類似百度文庫

  最近研究了一下在網頁上預覽文件(包括office文件和txt、pdf),發現用openoffice+FlexPlayer實現比較理想,就參考了https://blog.csdn.net/ITBigGod/article/details/80300177#commentBox這個部落格自己研究了一下。原始碼

AJAX實現圖片上傳和傳統ajax與jQuery AJAX;帶圖片的表單提交

  一、通過Servlet3.0和傳統的AJAX實現圖片上傳和預覽          此方法也適用於帶圖片的表單提交         上傳時預覽圖片    

php excel檔案線上走過的坑

首先感謝以往分享相關問題的小夥伴,從中收貨很多部分內容也是引用了前人的內容,希望對後來的人有幫助。 1.已經布好的服務如微軟的Office365等平臺服務 例項:http://technet.microsoft.com/zh-cn/library/jj21

HTML5 CSS3 經典案例:無外掛拖拽上傳圖片 支援與批量

上傳基本是專案中經常出現的,一般採用:1、form提交 2、flash3、html5form提交會重新整理頁面,很難做到非同步上傳;flash可能是用得比較多了,因為可以兼顧到幾乎所有的瀏覽器,我之前一直會用jquery的uploadify作為專案中的上傳工具,uploadi

java實現附件openoffice+swftools+flexpaper

先附上本人蔘考的文章,基於的 flexpaper版本 為 1.5,本人由於使用的是 2.1.9 ,故之後說明: 已經支援載入中文檔名 程式碼下載 1.概述 主要原理 1.通過第三方工具openoffice,將word、excel、pp

jq實現圖片上傳注意jq版本

一個jq實現上傳檔案預覽外掛uploadPreview.js /* *名稱:圖片上傳本地預覽外掛 *引數說明: Img:圖片ID;Width:預覽寬度;Height:預覽高度;ImgType:支援檔案型別;Callback:選擇檔案顯示圖片後回撥方法; *使用方法: &

HTML5 CSS3 經典案例:無外掛拖拽上傳圖片 支援與批量

效果圖1:效果圖2:好了,請允許我把圖片貼了兩遍,方便大家看效果了~可以看出我們的圖片的li的html其實還是挺複雜的,於是我把html文件做了一些修改:<span style="font-size:12px;"><body> <div id="uploadBox"> &

轉:最詳細的檔案線上openoffice+swftools+flexpaper

最詳細的檔案線上預覽(openoffice+swftools+flexpaper) 1.1    本人使用的都是最新的軟體包,版本見下圖: 1.2    已經支援載入中文檔名 1.3    程式碼下載 1.概述 主要原理 1.通過第三方工具openoffice,

js實現檔案的上傳和直接不經過服務端

js實現檔案的上傳,注意要使用BASE64編碼!<div class="bottom-border mt10"> <input type="hidden" id="imgcodeBusiness" value=""/> &l

JS上傳檔案和單張和多張

Section1單張上傳和預覽:思路:用<input type="file"/> 觸發onchange事件,取出e.target.files || e.target.dataTransfer.files, 將其轉為window.createObjectURL(fi