博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 自定义Camera 随笔
阅读量:5084 次
发布时间:2019-06-13

本文共 13466 字,大约阅读时间需要 44 分钟。

 

一、权限

 

这个false, 可以防止不让没有摄像头的设备安装
设置横屏

 

 

 

 

 

二、布局文件

 

这是用于放Camera的Frame框架

 

三、创建CameraPriview类,继承自SurfaceView。

 

 

 

 

package com.smarttpanorama.view;import java.io.IOException;import android.content.Context;import android.hardware.Camera;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{	private SurfaceHolder mHolder;	private Camera mCamera;		@SuppressWarnings("deprecation")	public CameraPreview(Context context,Camera camera) {		super(context);					mCamera = camera;		 // Install a SurfaceHolder.Callback so we get notified when the        // underlying surface is created and destroyed.		mHolder = getHolder();		mHolder.addCallback(this);				//deprecated setting,but required on Android versions prior to 3.0		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);	}	@Override	public void surfaceChanged(SurfaceHolder holder, int format, int width,			int height) {		// If your preview can change or rotate, take care of those events here.        // Make sure to stop the preview before resizing or reformatting it.        if (mHolder.getSurface() == null){          // preview surface does not exist          return;        }        // stop preview before making changes        try {            mCamera.stopPreview();        } catch (Exception e){          // ignore: tried to stop a non-existent preview        }        // set preview size and make any resize, rotate or        // reformatting changes here        // start preview with new settings        try {            mCamera.setPreviewDisplay(mHolder);			            mCamera.startPreview();                    } catch (Exception e){            Log.d("TAG", "Error starting camera preview: " + e.getMessage());        }    }	@Override	public void surfaceCreated(SurfaceHolder holder) {        if(null != mCamera)		  {					// The Surface has been created, now tell the camera where to draw the preview.	        		            try {                	mCamera.setPreviewDisplay(holder);//通过surfaceview显示取景画面                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }	        		        		            mCamera.startPreview();	     		  }	}	@Override	public void surfaceDestroyed(SurfaceHolder holder) {		  if(null != mCamera)		  {			   mCamera.setPreviewCallback(null); //!!这个必须在前,不然退出出错			   mCamera.stopPreview(); 			   mCamera.release();			   mCamera = null;		  }	}}

 

注意:在OnDestroy里已经写过release()释放过程。在Activity的Onpause里就不要再写一遍了,否则会出现 method called  after release()错误

 

三、CaptureActivity.java

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.content.pm.PackageManager;import android.graphics.PixelFormat;import android.hardware.Camera;import android.hardware.Camera.AutoFocusCallback;import android.hardware.Camera.CameraInfo;import android.hardware.Camera.Parameters;import android.hardware.Camera.PictureCallback;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.FrameLayout;import android.widget.Toast;import com.smarttpanorama.tools.Constant;import com.smarttpanorama.view.CameraPreview;import com.smarttpapers.smarttpanorama.R;public class CaptureActivity extends Activity {	public static final int MEDIA_TYPE_IMAGE = 1;//	public static final int MEDIA_TYPE_VIDEO = 2;	    private Camera mCamera;    private CameraPreview mPreview;      private Button BtnCapureLeft;    private Button BtnCapureRight;        private static int picCount = 0;        //将拍照结果存入文件    private PictureCallback mPicture = new PictureCallback() {	    @Override	    public void onPictureTaken(byte[] data, Camera camera) {	    	//得到文件名	        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);	        if (pictureFile == null){	            Log.d("TAG", "Error creating media file, check storage permissions");	            return;	        }	        //写入文件	        try {	            FileOutputStream fos = new FileOutputStream(pictureFile);	            fos.write(data);	            fos.close();	        } catch (FileNotFoundException e) {	            Log.d("TAG", "File not found: " + e.getMessage());	        } catch (IOException e) {	            Log.d("TAG", "Error accessing file: " + e.getMessage());	        }	        	        	        //显示在上边	    }	};        	@SuppressLint("NewApi")	@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_capture);                findViews();        if(checkCameraHardware(this) == false){        	Toast.makeText(this, "no camera on this device", Toast.LENGTH_LONG)        		.show();        }else{	        // Create an instance of Camera//	        mCamera = Constant.getCameraInstance(); //正常情况下这样调用。每种设备默认摄像头不同。但是现在默认要得到后置摄像头        	        		        //切换前后摄像头            int cameraCount = 0;            CameraInfo cameraInfo = new CameraInfo();            cameraCount = Camera.getNumberOfCameras();//得到摄像头的个数            Log.w("摄像头数量",cameraCount+"");                                    for(int i = 0; i < cameraCount; i++) {                Camera.getCameraInfo(i, cameraInfo);//得到每一个摄像头的信息                              //代表摄像头的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK后置                    if(cameraInfo.facing  == Camera.CameraInfo.CAMERA_FACING_FRONT) {                    	 Log.w("摄像头","前置");//                    	 mCamera.setPreviewCallback(null); //!!这个必须在前,不然退出出错//                        mCamera.stopPreview();//停掉原来摄像头的预览//                        mCamera.release();//释放资源//                        mCamera = null;//取消原来摄像头                          mCamera = Camera.open(i);//打开当前选中的摄像头//                        try {//                        	mCamera.setPreviewDisplay(holder);//通过surfaceview显示取景画面//                        } catch (IOException e) {//                            // TODO Auto-generated catch block//                            e.printStackTrace();//                        }//                        mCamera.startPreview();//开始预览                        break;                    }            }        	        	        	        		        // Create our Preview view and set it as the content of our activity.	        mPreview = new CameraPreview(this, mCamera);//	        mPreview = new CameraPreview(this);	        FrameLayout preview = (FrameLayout) findViewById(R.id.FrameCaptureView);	        preview.addView(mPreview);	        	        SetListener();        }    }	@Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }        @Override    protected void onPause() {        super.onPause();    }    			private void captureAndStartShowPic(){				  if(null != mCamera)		  {			mCamera.autoFocus(new AutoFocusCallback() {//自动对焦	            @Override	            public void onAutoFocus(boolean success, Camera camera) {	                if(success) {//	                   	//设置参数,并拍照//	                    Parameters params = camera.getParameters();//	                    params.setPictureFormat(PixelFormat.JPEG);//图片格式//	                    params.setPreviewSize(800, 480);//图片大小//	                    camera.setParameters(params);//将参数设置到我的camera	                    camera.takePicture(null, null, mPicture);//将拍摄到的照片给自定义的对象	                }	            }	        });						 // get an image from the camera//	         mCamera.takePicture(null, null, mPicture);	        //	        Intent intent = new Intent();//	        intent.setClass(this, ShowPicActivity.class);//	        startActivity(intent);		  }	}	        /** Check if this device has a camera */    private boolean checkCameraHardware(Context context) {        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){            // this device has a camera            return true;        } else {            // no camera on this device            return false;        }    }        /** Create a file Uri for saving an image or video */    private static Uri getOutputMediaFileUri(int type){          return Uri.fromFile(getOutputMediaFile(type));    }    /** Create a File for saving an image or video */    private static File getOutputMediaFile(int type){        // To be safe, you should check that the SDCard is mounted        // using Environment.getExternalStorageState() before doing this.        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(//        		Environment.DIRECTORY_DCIM), "Camera");                  Environment.DIRECTORY_PICTURES), "SmarttEye");                        // This location works best if you want the created images to be shared        // between applications and persist after your app has been uninstalled.        // Create the storage directory if it does not exist        if (! mediaStorageDir.exists()){            if (! mediaStorageDir.mkdirs()){                Log.d("Camera", "failed to create directory");                return null;            }        }        // Create a media file name        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());        File mediaFile;        if (type == MEDIA_TYPE_IMAGE){        	String FileName = mediaStorageDir.getPath() + File.separator +                    "IMG_"+ timeStamp + ".jpg";        	if(picCount % 2 == 0){        		Constant.FIRST_FILE_NAME = FileName;        	}else{        		Constant.SECOND_FILE_NAME = FileName;        	}            mediaFile = new File(FileName);            //        } else if(type == MEDIA_TYPE_VIDEO) {//            mediaFile = new File(mediaStorageDir.getPath() + File.separator +//            "VID_"+ timeStamp + ".mp4");        } else {            return null;        }        return mediaFile;    }            private void SetListener() {    	// Add a listener to the Capture button		BtnCapureLeft.setOnClickListener(    	    new View.OnClickListener() {    	        @Override    	        public void onClick(View v) {    	        	picCount++;    	        	    	        	captureAndStartShowPic();    	                	        }    	    }    	);				BtnCapureRight.setOnClickListener(    	    new View.OnClickListener() {    	        @Override    	        public void onClick(View v) {    	        	picCount++;    	        	    	        	captureAndStartShowPic();    	        }    	    }    	);	}        private void findViews() {		BtnCapureLeft = (Button) findViewById(R.id.BtnCaptureLeft);		BtnCapureRight = (Button) findViewById(R.id.BtnCaptureRight);	}}

 

 

 

在这个程序中。默认使用后置摄像头。所以注释掉了一部分代码。如果程序中需要手动切换摄像头。需要先release()掉之前的摄像头。再启动另一个。

前置后置摄像头的交换,要放在Activity里。否则在CameraPreview里切换了摄像头。之前的已经release掉了。Activity里再进行操作  就会报错(猜测)

 

 四、保存的路径

 

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(//           	  Environment.DIRECTORY_DCIM), "Camera");   这个路径 是系统拍照保存的路径。在根路经DCIM\Camera                  Environment.DIRECTORY_PICTURES), "SmarttEye");   这个是获取系统图片存放的路径。在根目录的“pictures”处

注意有些程序,在自定义camera拍完后,显示在图库中有延迟

 

五、拍照  得到的data  在这里。取到ImageView里边的时候。可以使用data。但是很多文章里,写会很费内存

 

private PictureCallback mPicture = new PictureCallback() {	    @Override	    public void onPictureTaken(byte[] data, Camera camera) {	    //得到文件名        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);	        if (pictureFile == null){	            Log.d("TAG", "Error creating media file, check storage permissions");	            return;	        }	        //写入文件        try {	            FileOutputStream fos = new FileOutputStream(pictureFile);	            fos.write(data);	            fos.close();	        } catch (FileNotFoundException e) {	            Log.d("TAG", "File not found: " + e.getMessage());	        } catch (IOException e) {	            Log.d("TAG", "Error accessing file: " + e.getMessage());	        }	        	        	        //显示在上边    }	};

 

 

 

 

转载于:https://www.cnblogs.com/maxiaodoubao/p/3899070.html

你可能感兴趣的文章
Web AppDomain
查看>>
JQuery创建规范插件
查看>>
AD 域服务简介(三)- Java 对 AD 域用户的增删改查操作
查看>>
Unity中Text渐变色,和Text间距
查看>>
P4932 浏览器
查看>>
Concurrency Kit 0.2.13 发布,并发工具包
查看>>
SQL Relay 0.50 发布,数据库负载均衡器
查看>>
Infinispan 5.3.0.Alpha1 发布
查看>>
设计模式学习笔记——原型模式(Prototype)
查看>>
算法普林斯顿
查看>>
Struts2之类范围拦截器和方法拦截器
查看>>
模型层(练习)
查看>>
XML解析技术研究(一)
查看>>
Qt 学习之路 :使用 QJson 处理 JSON
查看>>
NPOI操作Excel导入导出
查看>>
angularJS 移动端焦点图
查看>>
jvm 这我就能会了 擦
查看>>
实战技能:小小微信支付业务,何必虚惊一场
查看>>
17-1 djanjo进阶-路由,视图,模板
查看>>
Shell脚本8种字符串截取方法总结
查看>>