Android 自定义Camera 随笔

时间:2021-11-29 13:50:44
 

一、权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="false" /> 这个false, 可以防止不让没有摄像头的设备安装
<uses-feature android:name="android.hardware.camera.autofocus" /> <activity
android:name="com.smarttpanorama.ui.CaptureActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"> 设置横屏

二、布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/FrameCaptureView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> 这是用于放Camera的Frame框架 <LinearLayout
android:id="@+id/LinearCaptureLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@color/black"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.2">
<ImageView
android:layout_width="@dimen/camera_h"
android:layout_height="fill_parent"
android:background="@drawable/camera_top_left"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:gravity="left"> <ImageView
android:layout_width="@dimen/camera_h"
android:layout_height="fill_parent"
android:background="@drawable/camera_left_edge" /> </LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.11"
android:gravity="center"> <Button
android:id="@+id/BtnCaptureLeft"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_h"
android:background="@drawable/btn_camera" /> </LinearLayout>
</LinearLayout> <LinearLayout
android:id="@+id/LinearCaptureRight"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/black"
android:orientation="vertical"
android:gravity="right"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.2">
<ImageView
android:layout_width="@dimen/camera_h"
android:layout_height="fill_parent"
android:background="@drawable/camera_top_right"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.3">
<ImageView
android:layout_width="@dimen/camera_h"
android:layout_height="fill_parent"
android:background="@drawable/camera_right_edge" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.11"
android:gravity="center">
<Button
android:id="@+id/BtnCaptureRight"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_h"
android:background="@drawable/btn_camera" />
</LinearLayout>
</LinearLayout> <!-- <com.smarttpapers.shoot4video.customview.CameraPreview
android:id="@+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/LinearCaptureLeft"
android:layout_toLeftOf="@id/LinearCaptureRight"
android:keepScreenOn="true" /> --> <!--
android:layout_toRightOf="@id/LinearCaptureLeft"
android:layout_toLeftOf="@id/LinearCaptureRight"-->
<ImageView
android:id="@+id/IvCaptureLogo"
android:layout_width="@dimen/logo_w"
android:layout_height="@dimen/logo_h"
android:layout_marginTop="5dip"
android:layout_marginRight="@dimen/logo_margin_right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="@drawable/logo_smartteye" />
<ImageView
android:id="@+id/IvCaptureAngel"
android:layout_width="@dimen/angel_w"
android:layout_height="@dimen/angel_h"
android:layout_marginTop="10dip"
android:layout_marginLeft="@dimen/angel_margin_left"
android:background="@drawable/camera_angel" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="@dimen/logo_margin_right"
android:layout_marginBottom="8dip"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/BtnCapture180"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_btn_h"
android:background="@drawable/btn"
android:text="180°"
android:textSize="@dimen/camera_text_size" />
<Button
android:id="@+id/BtnCaptureFile"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_btn_h"
android:layout_marginLeft="10dip"
android:background="@drawable/btn"
android:text="File"
android:textSize="@dimen/camera_text_size" /> </LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginRight="@dimen/logo_margin_right"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dip"
android:layout_alignParentRight="true"> <Button
android:id="@+id/BtnCaptureSave"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_btn_h"
android:background="@drawable/btn"
android:text="Save"
android:textSize="@dimen/camera_text_size" />
<Button
android:id="@+id/BtnCaptureEdit"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_btn_h"
android:layout_marginLeft="10dip"
android:background="@drawable/btn"
android:text="Edit"
android:textSize="@dimen/camera_text_size" />
<Button
android:id="@+id/BtnCaptureMerge"
android:layout_width="@dimen/camera_h"
android:layout_height="@dimen/camera_btn_h"
android:layout_marginLeft="10dip"
android:background="@drawable/btn"
android:text="Merge"
android:textSize="@dimen/camera_text_size" /> </LinearLayout>
</RelativeLayout>

三、创建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());
} //显示在上边
}
};