import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
Camera.AutoFocusCallback, Camera.PictureCallback {
private TakePictureListener mListener;
/**
* This is a holder that holding a display surface.
*/
private SurfaceHolder mHolder = null;
private int sdk = 3;
/**
* This is a camera object using for connect/disconnect with the camera
* service,and so on.
*/
private Camera mCamera;
/**
* Perform inflation from XML and apply a class-specific base style. This
* constructor of View allows subclasses to use their own base style when
* they are inflating.
*
* @param context
* The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs
* The attributes of the XML tag that is inflating the view.
* @param defStyle
* The default style to apply to this view. If 0, no style will
* be applied (beyond what is included in the theme). This may
* either be an attribute resource, whose value will be retrieved
* from the current theme, or an explicit style resource.
*/
public CameraView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* Constructor that is called when inflating a view from XML. This is called
* when a view is being constructed from an XML file, supplying attributes
* that were specified in the XML file. This version uses a default style of
* 0, so the only attribute values applied are those in the Context's Theme
* and the given AttributeSet.
*
* @param context
* The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs
* The attributes of the XML tag that is inflating the view.
*/
public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* Simple constructor to use when creating a view from code.
*
* @param context
* The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public CameraView(Context context) {
super(context);
init();
}
public void init() {
if (mHolder == null) {
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
sdk = getSDKInt();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
try {
// rotate camera 90 degree on portrait mode
if (getContext().getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
if (sdk <= 4) {
// 1.5 & 1.6
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
mCamera.setParameters(parameters);
} else {
setDisplayOrientation(mCamera, 90);
}
}
} catch (Exception e) {
e.printStackTrace();
}
startPreview();
}
/**
* rotate camera with any degree, only available for SDK 5 and later
*
* @param camera
* @param angle
*/
private void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;
if (sdk <= 4)
return;
try {
if (sdk > 4 && sdk < 8) {
// parameters for pictures created by a Camera service.
Camera.Parameters parameters = mCamera.getParameters();
// 2.0, 2.1
downPolymorphic = parameters.getClass().getMethod(
"setRotation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(parameters, new Object[] { angle });
// Sets the Parameters for pictures from this Camera
// service.
mCamera.setParameters(parameters);
} else {
downPolymorphic = camera.getClass().getMethod(
"setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
}
} catch (Exception e) {
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// get Camera object.
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
} catch (RuntimeException e) {
e.printStackTrace();
releaseCamera();
} catch (IOException e) {
e.printStackTrace();
releaseCamera();
}
}
public void stopPreview() {
releaseCamera();
}
public void startPreview() {
if (mCamera != null) {
mCamera.startPreview();
mCamera.autoFocus(this);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
mCamera = null;
System.gc();
}
private int getSDKInt() {
// this is safe so that we don't need to use SDKInt which is only
// available after 1.6
try {
return Integer.parseInt(Build.VERSION.SDK);
} catch (Exception e) {
return 3; // default to target 1.5 cupcake
}
}
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success)
mCamera.takePicture(null, null, this);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
if (mListener != null) {
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
} catch (Exception e) {
e.printStackTrace();
}
mListener.onTakePicture(cameraBitmap);
}
startPreview();
}
public void setTakePictureListener(TakePictureListener listener) {
mListener = listener;
}
public interface TakePictureListener {
public void onTakePicture(Bitmap bitmap);
}
}
分享到:
相关推荐
纽约证券交易所上市代号:A) 日前推出一款特别为数码相机在弱光环境下提供照明的、距离长达3米的自动对焦高亮度发光二极管(LED, Light-Emitting Diode)。安捷伦的ASMT-FJ10是目前市场上亮度最高的自动对焦LED,在20...
据称这是目前市场上亮度、封装尺寸的自动对焦LED,在20mA时的标准输出为18堪(candela),尺寸为4.8×4.8mm。 传统的相机通常使用红外线二极管(IR LED)作为弱光环境下的自动对焦光源,因为只有IR LED才能提供这类...
据称这是目前市场上亮度最高、封装尺寸最小的自动对焦LED,在20mA时的标准输出为18堪(candela),尺寸为4.8×4.8mm。 传统的相机通常使用红外线二极管(IR LED)作为弱光环境下的自动对焦光源,因为只有IR LED才能...
安捷伦科技有限公司(Agilent Technologies)日前推出一款特别为数码相机在弱光环境下提供照明的、距离长达3米的自动对焦高亮度发光二极管(LED, Light-Emitting Diode)。安捷伦的ASMT-FJ10是目前市场上亮度最高的...
这个紧凑的绿色LED灯相当容易安装,除了拥有业界最高亮度以及最紧凑的封装,并面向需要能够在黑暗环境中进行自动对焦功能的应用工程师提供了一个具备高成本效益的解决方案。 相较于其他颜色,数码相机对绿色的反应...
这个紧凑的绿色LED灯相当容易安装,除了拥有业界最高亮度以及最紧凑的封装,并面向需要能够在黑暗环境中进行自动对焦功能的应用工程师提供了一个具备高成本效益的解决方案。 相较于其他颜色,数码相机对绿色的反应...
手动调整表格大小调的太累了,所以自己封装了QTableView, 支持自动调整每一行和每一列的大小,只需要传入宽高和头就可以
框架的思想: 解决我们测试过程中的问题:大量的重复步骤,用自动化来实现 1)配置和程序的分离 2)测试数据和程序的分离 3)不懂编程的人员可以方便使用:使用的...6)框架中不要有重复的代码,实现高度的封装和复用
软件自动安装器,可自由编辑,自动安装软件,可用于系统封装
Allegro封装自动生成Allegro封装自动生成Allegro封装自动生成
封装系统,自动删除c:\drivers文件夹.可将此文件放在除SYSPREP文件夹以外的任何地方,在封装工具中首次进系统运行此文件,就能删除c:\drivers文件夹,完成后自动删除自身,所以不用再进行删除此文件的步骤.
基于appium和python语言封装的自动化测试库
最新运营级一键IOS免签分发系统源码下载 带绿标+支持在线封装app 【运营版】的在线IOS免签封包分发平台,一键IOS免签,支持在线 封装app的分发系统源码,所有功能可进行二次开发, 基于目前主流的分发平台系统二开而...
某站8000元APP封装系统源码app误报毒app可上传+自动实现5分钟随机更换包名和签名+视频安装教程 2022-07-10 永久尊享 55 推广 PS 本源码提供给大家学习研究借鉴美工之用,请勿用于商业和非法用途,无任何技术支持! ...
某站8000元app封装系统 app误报毒app可上传 自动实现5分钟随机更换包名和签名+视频教程 程序可实现安卓app五分钟自动打包更换包名和签名 也可以上传打包好的apk五分钟自动更换包名和签名, 自动覆盖原下载路径 ...
CADENCE 封装自动生成器(已破解),直接解压既可用.
Myeclipse10.7 封装类 自动设置 Get Set方法中文注释设置 更改后可根据封装类熟悉注释,自动设置get set方法中文注释,以便调用时查看备注
2022新版本运营级分发平台一键IOS免签支持在线封装app分发源码 支持免签绿标 2022新版本运营级分发平台一键IOS免签支持在线封装app分发源码 支持免签绿标 可能有后门,所以分享出来给大家看一下,前端去掉了两个加密...
基于AFNetworking封装,能够自动取消网络请求任务的工具