一、权限

<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. Codeforces 938E Max History:排列 + 逆元【考虑单个元素的贡献】

    题目链接:http://codeforces.com/problemset/problem/938/E 题意: 定义f(a): 初始时f(a) = 0, M = 1. 枚举i = 2 to n,如果a ...

  2. ansible实现发布、回滚功能

    ansible的两篇博客,本来是打算合二为一的,发现只用一篇写,嗯,好鬼长.... 一向秉承简单为美的我于是忍痛割爱,一分为二了 ansible实现升级发布.回滚功能 1.应用场景 在实际生产环境中, ...

  3. review13

    Date与Calendar类 Date类和Calendar类属于java.util包. Date类 1.使用无参数构造方法 使用Date类的无参构造方法创建的对象可以获取本机的当前日期和时间,例如: ...

  4. js 实现自动调出键盘

    在app中,在页面加载完成之后,给输入框添加一个focus,不能自动调出软键盘,可以用以下方式实现: //触发键盘 $("#content").on("touchstar ...

  5. 调用摄像头并将其显示在UGUI image上自适应屏幕大小

    参考链接:http://www.cnblogs.com/Erma-king/p/5869177.html 不过该博主是竖屏,我的是横屏 代码修改: using UnityEngine; using S ...

  6. php 实现微信模拟登陆、获取用户列表及群发消息功能示例

    本文实例讲述了php实现微信模拟登陆.获取用户列表及群发消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  7. 团队小组NABCD(通用作业和个人作业)特点

    NABCD框架(通用作业和个人作业): N(need,需求): 你的创意解决了用户的什么需求? 使用户能够很好的区分作业情况,将班里所有同学的作业和自己私人的作业分开,通用作业指在一个班一同上课的公共 ...

  8. hdu 5242 Game(树链剖分,贪心¥)

    Game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. BW ON HANA 业务模型关系与数据取数

    在接到业务需求之后,我认为重要的是理清楚自己该做什么.来实现业务.由于不了解业务,还是走了很多弯路.本可以不用这么做,还是这么做了.自然你最傻瓜的按照用户的方式去实现是没有问题的. 会使后面的人难以维 ...

  10. html5 video 监听播放结束. 最好获取html标签而不是id。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...