android camera 自定义开发
1、检测是否有摄像头
/** 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;
}
}
2.3后提供了一个方法可获取摄像头的个数:Camera.getNumberOfCameras()
2、访问获取camera对象
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
Caution: Always check for exceptions when using Camera.open(). Failing to check for exceptions if the camera is in use or does not exist will cause your application to be shut down by the system.
每次使用时都需要检测camera是否可用,否则很可能会让你的应用强制关闭。
在2.3或更高版本中,可以使用Camera.open(int)方法来请求打开哪个摄像头,如果有多前置摄像头,open(1)则是打开前置摄像头,open(0)是打开后置摄像头
3、检查摄像头的特性
Once you obtain access to a camera, you can get further information about its capabilities using the Camera.getParameters() method and checking the returned Camera.Parameters object for supported capabilities. When using API Level 9 or higher, use the Camera.getCameraInfo() to determine if a camera is on the front or back of the device, and the orientation of the image.
4、Creating a preview class
为了让用户可以更好的排出自己想要的照片或视频,用户就需要知道当前摄像头获取到的图片或视频数据,这个时候就需要一个组件来展示让用户预览这些数据。SurfaceView就是这么一个可以预览摄像头当前获取到的图片或视频的组件。
下面的代码演示了如何创建一个基本的预览框,这个预览框可以放在任何的layout内。 这个类实现了SurfaceHolder.Callback 这个接口以用来监听预览组件的创建和销毁。
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera; 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);
} public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
} public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
} public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// 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());
}
}
}
如果你想给摄像头设置一个特定大小的预览框,需要在surfaceChanged()这个方法内进行修改,当设置尺寸时需要在getSupportedPreviewSizes()的范围内,而不应该使用setPreviewSize()方法随意设置。
5、Placing preview in a layout
预览图必须要与其他的api共同使用才有效果,无论是拍照还是摄像的预览都需要依赖于activity进行预览展示,下面就是一个最基本的预览图的layout及activity的创建方式,在下面的示例中,FrameLayout 是摄像头预览框的容器。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/> <Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
On most devices, the default orientation of the camera preview is landscape. This example layout specifies a horizontal (landscape) layout and the code below fixes the orientation of the application to landscape. For simplicity in rendering a camera preview, you should change your application's preview activity orientation to landscape by adding the following to your manifest.
在大部分的设备中,默认的摄像头预览方向为横屏,下面我们也就直接使用横屏的方式创建Activity
<activity android:name=".CameraActivity"
android:label="@string/app_name" android:screenOrientation="landscape">
<!-- configure this activity to use landscape orientation --> <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:预览不是一定要用横屏,从2.2(API Level8)版本开始,你可以使用setDisplayOrientation()方法来设置预览图片的方向,为了改变预览的方向,你还需要在surfaceChanged()方法中嫌停止预览,然后更改方向后再重新启动预览。
在你的Activity中,将预览的组件加入FrameLayout中,当Activity处与pause 或者 destroy时,你需要释放Camera的资源以备其他应用使用。
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
}
6、Capturing pictures
当你创建好了预览组件并且将它显示在屏幕上了,你的应用就已经做好了拍照的准备了。这个时候你需要为你的程序创建Listeners来对Camera进行监控。
为了拍摄下图片,你需要调用Camera.takePicture()方法,这个方法有3个参数,下面是一个简单的实现方式
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: " +
e.getMessage());
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());
}
}
};
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
至此Camera的基本用法已结束。
android camera 自定义开发的更多相关文章
- Android Camera系列开发 (二)通过Intent录制视频
Android Camera系列开发 (二)通过Intent录制视频 作者:雨水 2013-8-18 CSDN博客:http://blog.csdn.net/gobitan/ 概述 使用Camera ...
- 【转】Android Camera 相机开发详解
在Android 5.0(SDK 21)中,Google使用Camera2替代了Camera接口.Camera2在接口和架构上做了巨大的变动, 但是基于众所周知的原因,我们还必须基于 Android ...
- Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能
Android Camera开发系列(下)--自定义Camera实现拍照查看图片等功能 Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 上 ...
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...
- Android Camera开发:周期性循环自动聚焦auto focus挂掉原因分析(preview is not enabled)
参考:Android Camera开发:扫描二维码,周期性循环自动聚焦auto focus挂掉原因分析(preview is not enabled) 最近做Android人脸识别时,camera在自 ...
- Android Camera子系统之Linux C应用开发人员View
Android Camera HAL通过V4L2接口与内核Camera Driver交互.本文从Linux应用开发人员的角度审视Android Camera子系统. V4L2应用开发一般流程: 1. ...
- Android H5混合开发(2):自定义Cordova插件
前言 Cordova虽然定义了很多基础的插件,供H5端使用原生设备的功能. 但是,如果业务相关的功能,需要提供给H5端使用,那么,就需要我们自定义插件了. 这个"自定义"不是指由A ...
- 使用AndroidFrameworks开发和应用隐藏类 or Android使用自定义framework开发与应用
Android眼下代表系统的开源手机操作系统已经更新到4.0.3版本号.由于其开源特性.使得操作系统本身所具有的最大的灵活性,但同时也引起的版本号的多样性,市场上出现的是手机厂商或ROM.可是怎样开发 ...
- Android音视频开发(1):H264 基本原理
前言 H264 视频压缩算法现在无疑是所有视频压缩技术中使用最广泛,最流行的.随着 x264/openh264 以及 ffmpeg 等开源库的推出,大多数使用者无需再对H264的细节做过多的研究,这大 ...
随机推荐
- 还是要好好研究开源的php
听说facebook是php写的,还是要静下心来好好研究一番的嘛,踏踏实实点点滴滴的做起来!加油
- HDU1054 Strategic Game——匈牙利算法
Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...
- VMware Workstation+Linux+Xshell+Xftp+MySQL+SQLyog 配置
这些天在搞这些个东西做项目,配置较繁,这里记下安装过程中的要点. 1.VMware Workstation 主要是 NAT 方式联网的问题,详述如下,来自网络. NAT 配置那里注意网关,虚拟机中网关 ...
- 关闭Win10自带的 Windows Defender
1.按下Win+R,输入gpedit.msc 2.进入组策略,选择计算机配置>管理模板>Windows 组件>Windows Defender 3.双击"关闭 Window ...
- Oracle 12c 的新功能:模式匹配查询
模式匹配SQL 在一系列的数据行中辨识出某种模式是一种广泛需求的功能,但在以前的SQL中是不可能的.有许多解决方法,但这些都很难写,很难理解,而且执行效率低下.在Oracle数据库中,从12c开始,你 ...
- cmd运行的程序的工作目录
如图所示,cmd通过输入自己编写的程序的实际路径,或者将程序放在环境变量中然后在cmd中执行,用start执行,该程序运行时的工作目录都是cmd当前所在目录:在cmd中输入该程序的快捷方式执行该程序, ...
- Select语句也会引起死锁
项目上线,准备验收前出现了一个严重的问题:很多select语句作为死锁的牺牲,大部分报表无法打开.这个问题影响范围很大所有的报表都无法访问,而我们的报表是放在电视上面轮播的,电视放在工厂里面,所以出现 ...
- aa3
var geoCoordMap = { "海门":[121.15,31.89], "鄂尔多斯":[109.781327,39.608266], "招远 ...
- 陨石坑之webapi使用filter
首先为什么说这是一个坑,是因为我们在webapi中使用filter的时候也许会先百度一下,好吧,挖坑的来了,我看了好几篇文章写的是使用System.Web.Mvc.Filters.ActionFilt ...
- C#获取当前路径的7种方法
总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName ...