Android 自定义Camera 随笔

 

一、权限

<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());
	        }

	        //显示在上边
    }
	};

Android 自定义Camera 随笔

时间: 2024-12-19 16:55:22

Android 自定义Camera 随笔的相关文章

Android自定义Camera

Build A CAMERA(创建一个自定义的Camera) 一些开发人员需要一个(为应用程序定制或提供特殊功能)的相机用户界面(自定义相机).创建一个定制的相机活动需要更多的代码,但它可以为你的用户提供更令人信服的体验. 为您的应用程序创建自定义相机接口的一般步骤如下: 1.        检测和访问摄像机-创建代码,以检查是否存在摄像头和允许访问. 2.        创建一个预览类,创建一个摄像机预览类继承SurfaceView实现SurfaceHolder接口.这类用于相机预览. 3. 

Android 实例讲解自定义Camera拍照和预览以及前后置摄像头切换

上一篇博文讲解了怎么去调用本地图片和调用系统拍照图片(http://blog.csdn.net/a123demi/article/details/40003695)的功能. 而本博文将通过实例实现自定义Camera的功效.具体功能如下: 1.实现自定义Camera拍照: 2.实现前后置摄像头的切换: 3.实现Camera拍照后图片缩小显示以及正常预览: 4.实现Camera拍照后图片保存: 在具体实现代码之前,我们先来了解一下Android api对实现自定义Camera的介绍. 根据api的介

android 自定义照相机Camera黑屏 (转至 http://blog.csdn.net/chuchu521/article/details/8089058)

对于一些手机,像HTC,当自定义Camera时,调用Camera.Parameters的 parameters.setPreviewSize(width, height)方法时,如果width和height为奇数情况下,则会出现黑屏现象,解决办法可参考SDK提供的ApiDemos中关于Camera的 例子: List<Size> sizes = parameters.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreview

Android自定义照相机 预览拍照 切换前后置摄像头

Android提供了Camera来控制拍照,步骤如下:(1)调用Camera的open()方法打开相机.(2)调用Camera的getParameters()获取拍照参数,该方法返回一个Cmera.Parameters对象.(3)调用Camera.Parameters对象对照相的参数进行设置.(4)调用Camera的setParameters(),并将Camera.Parameters对象作为参数传入,这样就可以对拍照进行参数控制,Android2.3.3以后不用设置.(5)调用Camerade

Android 自定义Gallery浏览图片

之前写的<Android ImageSwitcher和Gallery的使用>一文中提到我在教室一下午为实现那个效果找各种资料.期间在网上找了一个个人觉得比较不错的效果,现在贴图上来: 其实这个效果使用的知识点就是图像的获取.创建.缩放.旋转.Matrix类.Canvas类等,另外就是自定义的Gallery控件. 相信大家都期待马上上代码了吧,嘻嘻.(注释比较多,相信大家都能看懂.) main.xml: <?xml version="1.0" encoding=&quo

Android自定义相机超详细讲解

Android自定义相机超详细讲解 转载请标明出处: http://blog.csdn.net/vinicolor/article/details/49642861: 由于网上关于Android自定义相机的文章写得不是太详细,Google官方的文档又说得不太容易理解,所以今天我来详细讲解一下Android自定义相机. 这篇文章主要写给一些刚刚接触Android的那些看官方API困难以及不太了解Android机制的同学们,所以熟练开发者可以绕道了. 最近在使用Camera类的时候发现居然被弃用了,

Android自定义照相机注意事项

1.设置预览方向myCamera.setDisplayOrientation(90) 2.设置照片方向parameters.setRotation(90); 3.设置预览显示和照片的分辨率 // 设置照片分辨率 List<Camera.Size> previewSizeList = parameters .getSupportedPreviewSizes(); int previewWidth = 0; int previewHeight = 0; for (int i = 0; i <

Android 自定义ViewGroup手把手教你实现ArcMenu

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37567907 逛eoe发现这样的UI效果,感觉很不错,后来知道github上有这么个开源项目~~~~当然本篇不是教你如何使用这个开源项目,而是教你如何自己通过自定义ViewGroup写这样的效果,自定义ViewGroup也是我的痛楚,嘿嘿,希望以此可以抛砖引玉~~ 效果图: 1.实现思路 通过效果图,会有几个问题: a.动画效果如何实现 可以看出动画是从顶点外外发射的,可能有人

Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)

Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是通过自定义的方式来实现手机照相的功能. 创建一个项目:FingerTakePicture 首先来搞一下界面: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&qu