package com.Aina.Android;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* com.Aina.Android Pro_Camera
*
* @author Aina.huang E-mail: 674023920@qq.com
* @version 创建时间:2010 Jul 7, 2010 2:50:15 PM 类说明
*/
public class PreView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder = null;
private Camera mCamera = null;
private Bitmap mBitmap = null;
public PreView(Context context) {
super(context);
Log.i("TAG", "PreView()");
// TODO Auto-generated constructor stub
holder = this.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("TAG", "surfaceChanged");
// TODO Auto-generated method stub
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);//设置图片格式
parameters.setPreviewSize(320, 480);//设置尺寸
// parameters.setPictureSize(320, 480);//设置分辨率
mCamera.setParameters(parameters);
mCamera.startPreview();//开始预览
}
public void surfaceCreated(SurfaceHolder holder) {
Log.i("TAG", "surfaceCreated");
// TODO Auto-generated method stub
mCamera = Camera.open();//启动服务
try {
mCamera.setPreviewDisplay(holder);//设置预览
} catch (IOException e) {
mCamera.release();//释放
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("TAG", "surfaceDestroyed");
// TODO Auto-generated method stub
mCamera.stopPreview();//停止预览
mCamera = null;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
if(mCamera !=null){
mCamera.takePicture(null, null,pic);
}
}
return super.onKeyDown(keyCode, event);
}
//拍照后输出图片
public Camera.PictureCallback pic = new Camera.PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File f = new File(android.os.Environment.getExternalStorageDirectory()+"/camera1.jpg");
try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
os.flush();
os.close();
Canvas canvas = holder.lockCanvas();
canvas.drawBitmap(mBitmap, 0, 0, null);
holder.unlockCanvasAndPost(canvas);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
package com.Aina.Android;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* com.Aina.Android Pro_Camera
*
* @author Aina.huang E-mail: 674023920@qq.com
* @version 创建时间:2010 Jul 7, 2010 2:50:15 PM 类说明
*/
public class PreView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder = null;
private Camera mCamera = null;
private Bitmap mBitmap = null;
public PreView(Context context) {
super(context);
Log.i("TAG", "PreView()");
// TODO Auto-generated constructor stub
holder = this.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("TAG", "surfaceChanged");
// TODO Auto-generated method stub
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);//设置图片格式
parameters.setPreviewSize(320, 480);//设置尺寸
// parameters.setPictureSize(320, 480);//设置分辨率
mCamera.setParameters(parameters);
mCamera.startPreview();//开始预览
}
public void surfaceCreated(SurfaceHolder holder) {
Log.i("TAG", "surfaceCreated");
// TODO Auto-generated method stub
mCamera = Camera.open();//启动服务
try {
mCamera.setPreviewDisplay(holder);//设置预览
} catch (IOException e) {
mCamera.release();//释放
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("TAG", "surfaceDestroyed");
// TODO Auto-generated method stub
mCamera.stopPreview();//停止预览
mCamera = null;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
if(mCamera !=null){
mCamera.takePicture(null, null,pic);
}
}
return super.onKeyDown(keyCode, event);
}
//拍照后输出图片
public Camera.PictureCallback pic = new Camera.PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File f = new File(android.os.Environment.getExternalStorageDirectory()+"/camera1.jpg");
try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
os.flush();
os.close();
Canvas canvas = holder.lockCanvas();
canvas.drawBitmap(mBitmap, 0, 0, null);
holder.unlockCanvasAndPost(canvas);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
Java代码
package com.Aina.Android;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
public class Test extends Activity {
/** Called when the activity is first created. */
private PreView pv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
pv = new PreView(this);
setContentView(pv);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return pv.onKeyDown(keyCode, event);
}
}
package com.Aina.Android;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
public class Test extends Activity {
/** Called when the activity is first created. */
private PreView pv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
pv = new PreView(this);
setContentView(pv);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return pv.onKeyDown(keyCode, event);
}
}
Java代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Aina.Android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Test"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>
分享到:
相关推荐
android_Camera框架分析
`Camera`类的使用涉及到权限设置,如需访问相机,需要在`AndroidManifest.xml`中声明`<uses-permission android:name="android.permission.CAMERA" />`。 ### 摄像头预览 在Android相机应用中,首先需要创建一个...
Android_CameraX_+_OpenGL。虽然_CameraX_已经封装了大部分对相机操作,_GLCameraX
### Android_Camera框架详解 #### 一、框架概览 Android_Camera框架是Android系统中负责处理摄像头功能的核心组件,它不仅支持预览(viewfinder),还能够实现拍照等基本操作。框架设计精巧,分为客户端与服务器两...
在Android系统中,相机(Camera)是至关重要的组件,它允许开发者创建丰富的应用程序,提供拍照、录像等功能。本文将深入探讨Android Camera架构,包括其组件、工作流程以及如何通过编程接口进行交互。 Android ...
【标题】"android_CameraDemo-master_串口_android串口开发_zerolon_android_cowiwz_" 指向的是一个Android应用项目,该项目专注于在三星Android平台上进行摄像头和串口通信的开发。"zerolon" 和 "cowiwz" 可能是...
这个“android_camera2_api_video_app-master_androidcamera_videocapture_”项目显然关注的是利用Camera2 API实现视频录制和屏幕捕捉功能。以下是对这个项目的详细分析,以及相关的知识点讲解。 1. **Camera2 API*...
"android_camera_demo.zip"这个压缩包提供了一个示例代码,帮助开发者了解如何实现这些功能。下面我们将深入探讨其中涉及的技术点。 1. **Android CameraX库**: Android CameraX是一个官方推荐的高级相机库,它为...
Android的Camera系统是一个复杂而精细的设计,涉及到多个层次的组件和接口交互。在这个系统中,Camera功能被划分为客户端和服务器两部分,基于Android的Binder机制实现进程间通信。下面我们将详细探讨Android Camera...
android_camera_framework_stream.pdf
android_camera_framework_stream汇编.pdf
在Android平台上,开发一款能够进行视频录制的应用是一个常见的任务,而`Android-Camera-master`项目则提供了一个关于如何实现这一功能的源代码示例。在这个项目中,开发者使用了Android的Camera API来捕获和记录...
在Android平台上,Camera API是开发者用来实现相机功能的关键接口。Android_Camera分析主要涉及以下几个核心知识点: 1. **Camera API版本**: Android提供了两种Camera API:Camera API1(旧版)和Camera API2...
在Android平台上,原生相机(Native Camera)开发是一项核心技能,尤其对于那些希望深度定制相机功能的开发者来说。"camera_salmonrhx_camera_android_原生camera开发_"这个项目显然是一个专注于Android原生相机API...
本文将深入探讨“Android_Camera_Example”项目,这是一份适用于Android开发者学习和参考的示例代码,它展示了如何在Android应用中集成并操作相机。 首先,我们需要了解Android相机API的基础知识。Android提供了两...
### Android Camera Framework Stream详解 #### 一、概述 在深入探讨`android_camera_framework_stream`之前,我们先简要了解一下此主题的基本概念与重要性。在Android系统中,摄像头功能是移动设备的重要组成部分...
Winsoft_Camera_for_Android_3.5_Delphi_XE5_10.3_Cracked_Downloadly.ir.rar
android_camera_framework_stream[汇编].pdf
Android Camera Demo是用于展示如何在Android设备上使用摄像头功能的示例代码。这篇博客文章(http://blog.csdn.net/amoscxy/article/details/79117522)详细介绍了Android Camera API的基础知识,并提供了一个名为...