一、权限

<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 随笔的更多相关文章

  1. android自定义camera以及uri和文件路径之间的转换

    相对直接调用系统的camera,这种方法使用得相对还少一些.根据api文档,步骤如下: 定义一个预览类 可以参照<android高薪之路>这本书上面,有这种方法的一种完整实现 而对应的ac ...

  2. Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能

    Android Camera开发系列(下)--自定义Camera实现拍照查看图片等功能 Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 上 ...

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

    对于一些手机,像HTC,当自定义Camera时,调用Camera.Parameters的 parameters.setPreviewSize(width, height)方法时,如果width和hei ...

  4. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  5. Android自定义相机拍照、图片裁剪的实现

    最近项目里面又要加一个拍照搜题的功能,也就是用户对着不会做的题目拍一张照片,将照片的文字使用ocr识别出来,再调用题库搜索接口搜索出来展示给用户,类似于小猿搜题.学霸君等app. 其实Android提 ...

  6. 介绍 Android 的 Camera 框架

    总体介绍 Android Camera 框架从整体上看是一个 client/service 的架构,有两个进程:一个是 client 进 程,可以看成是 AP 端,主要包括 JAVA 代码与一些 na ...

  7. android——创建camera应用(译)

     只是选择相机部分来翻译.下面是主要内容 有些开发者可能需要Camera的接口,来定制自己程序的外观和特殊功能.创建自定义的Camera界面比使用using an Intent需要编写更多的代码,但是 ...

  8. Android 自定义View修炼-Android 实现自定义的卫星式菜单(弧形菜单)View

    一.总述 Android 实现卫星式菜单也叫弧形菜单的主要要做的工作如下:1.动画的处理2.自定义ViewGroup来实现卫星式菜单View (1)自定义属性       a. 在attrs.xml中 ...

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

    Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是 ...

随机推荐

  1. 打包的时候遇上找不到dll文件错误

    1.保证dll文件和EXE文件处于同级目录下 我是在EXE同级文件目录下建立了一个Plugins文件并把dll文件夹放在这里面 2.但是因为建立的目录是x86_64,所以如果打包成windows平台选 ...

  2. mac实用脚本

    安全清倒废纸篓(AppleScript) on run {input, parameters} set title to "安全清倒废纸篓" set trashIsEmptyMes ...

  3. hzau 1199 Little Red Riding Hood

    1199: Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 918  Solved: 158[Submit ...

  4. 关于C++中的pow小记(转)

    昨天在敲一个数位DP的问题,但是用到了这个坑D的问题,找了半天错,还以为又是什么奇怪的算法,结果发现思路一致,然后自己各种YY修改,最后不得不和正确答案比对,但是最后发现标准答案和自己的想法几乎一模一 ...

  5. svg札记

    1.人老了,有些事情太容易忘记了,这里做下笔记,供参考,for self for you. 2.源于地图监控,建筑级别各大地图商的api(高德.百度.腾讯等)已经足够使用,唯独室内图这块还差点. 3. ...

  6. linux 部署python2.7

    tar xvf Python-.tar.bz2 mkdir /usr/local/python27 ls cd Python- ./configure --prefix=/usr/local/pyth ...

  7. 使用饿了么ui表单验证报错: [Element Warn][Form]model is required for validat

    [Element Warn][Form]model is required for validat 如文末的完整例子: 该提示说的是 form表单需要一个绑定一个 对象(使用:model=" ...

  8. Android Studio 学习 - Intent学习

    今天开始仔细的学习Intent. 看的比较多,还在消化中,后续继续完善本篇笔记……

  9. 基于JQ的简单左右轮播图

    // 轮播图 主要实现思想: a.第一层div,设置overflow为hidden. b.里面是一个ul,每个li里面有个img或者为每个li设置背景图片也可以. c.li设置为左浮动,排成一行,还有 ...

  10. 执行.class文件

    java packageName.className即可 但是注意,如果是有包的,这段指令一定是packageName的上层目录(即bin目录)执行!