Camera的使用
Posted Fishbonell
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Camera的使用相关的知识,希望对你有一定的参考价值。
本节主要记录在android中不使用默认的拍照应用,而是自启摄像头,并不断刷新屏幕实现预览的功能。
AndroidManifest.xml:
<activity android:name=".CameraActivity" android:screenOrientation="portrait"/>
CameraActivity.java:
public class CameraActivity extends Activity implements mBaseUiFragment.OnUiFragmentJumpToListener { CameraFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { //请求窗口特性:无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //添加窗口特性:全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.camera_activity_layout); initFragment(); } private void initFragment(){ if (fragment == null){ fragment = new CameraFragment(); } FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.camera_framelayout, fragment, CameraFragment.FRAGMENT_TAG); fragmentTransaction.commit(); } @Override public void jumpToFragment(Class<? extends Fragment> fragmentClass, String toFragmentTag) { } @Override public void jumpToFragment(Class<? extends Fragment> fragmentClass, String toFragmentTag, Bundle args) { } @Override public void fragmentBack() { } }
CameraFragment.java:
public class CameraFragment extends mBaseUiFragment{ static public String FRAGMENT_TAG = "CameraFragment"; private final static int REQUEST_DELETE_PHOTO = 1; // 相机类 Camera mCamera; //显示界面 private SurfaceHolder mSurfaceHolder; // 前后摄像头的选择 private int switchCamera = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.camera_layout, null); initView(); return rootView; } private void initView(){ SurfaceView mSurfaceView = (SurfaceView) rootView.findViewById(R.id.camera_surfaceView); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback( new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { //当SurfaceView的视图层级结构被放到屏幕上时候,连接Camera和Surface try { if (mCamera != null) { mCamera.setPreviewDisplay(holder); } } catch (Exception e) { // TODO: handle exception } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //当Surface首次显示在屏幕上时候,设置好相机参数,开始预览 if (mCamera == null) { return; } Camera.Parameters parameters = mCamera.getParameters(); Camera.Size s = getBestSupportedSize(parameters.getSupportedPreviewSizes(), width, height); parameters.setPreviewSize(s.width, s.height); parameters.setPictureSize(s.width, s.height); mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(270); try { mCamera.startPreview(); } catch (Exception e) { // TODO: handle exception mCamera.release(); mCamera = null; } } @Override public void surfaceDestroyed(SurfaceHolder holder) { //当视图从屏幕上移除的时候,停止预览 if (mCamera != null) { mCamera.stopPreview(); } } } ); } /******************************************] * * 穷举法找出具有最大数目像素的尺寸 * * @param sizes * @param width * @param height * @return */ public Camera.Size getBestSupportedSize(List<Camera.Size> sizes,int width,int height) { Camera.Size bestSize = sizes.get(0); int largestArea = bestSize.width*bestSize.height; for (Camera.Size s :sizes) { int area =s.width*s.height; if (area > largestArea) { bestSize=s; largestArea = area; } } return bestSize; } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("onPause"); //程序中止暂停时,释放Camera if (mCamera!=null) { mCamera.release(); mCamera=null; } } @SuppressLint("NewApi") @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); //程序运行时,打开Camera if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { mCamera = Camera.open(switchCamera); }else { mCamera = Camera.open(); } } }
Done
以上是关于Camera的使用的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Camera 2 API Android 提高捕获图像的质量?
如何在 Android 中提高 Camera 2 的图像质量?