android 简单照相机

本文详细介绍了使用Android SDK实现简单照相机应用的过程,包括初始化摄像头、预览图像、自动对焦以及拍照并保存图片的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyCameraDemo.java
android简单照相机的实现
Camera操作类及SurfaceView捕获图像组件

package org.lxh.demo;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MyCameraDemo extends Activity {

	private SurfaceView surfaceview=null;
	private Button button=null;
	private Camera camera=null;
	private SurfaceHolder holder=null;
	//定义一个摄像头资源捕获图像被销毁的时间
	private boolean previewRunTime=true;
	
	//@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.surfaceview=(SurfaceView)super.findViewById(R.id.surface);
		this.button=(Button)super.findViewById(R.id.but);
		this.holder=this.surfaceview.getHolder();
		this.holder.addCallback(new MySurfaceViewCallback());
		this.holder.setFixedSize(500, 350);//捕获范围
		this.button.setOnClickListener(new OnClickListenerImpl());
	}
	
	/**
	 * 拍照
	 * @author yuhang
	 *
	 */
	private class OnClickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if(MyCameraDemo.this.camera!=null){
				MyCameraDemo.this.camera.autoFocus(new MyAutoFocusCallback());//实现自动对焦功能
			}
		}
		
	}
	/**
	 * 预览
	 * @author yuhang
	 *
	 */
	private class MySurfaceViewCallback implements SurfaceHolder.Callback{

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
			
		}

		//@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			MyCameraDemo.this.camera.open();//取得第一个摄像头
			
			/**To create windows on a different display, 
			 * you need to obtain a WindowManager for that Display.
			 * 
			 * Returns
			 * The display that this window manager is managing.
			 */
			WindowManager manager=(WindowManager)MyCameraDemo.this.getSystemService(Context.WINDOW_SERVICE);
			Display display=manager.getDefaultDisplay();
			
			//Provides information about the size and density of a logical display.
			Parameters param=MyCameraDemo.this.camera.getParameters();
			
			param.setPreviewSize(display.getWidth(), display.getHeight());
			param.setPreviewFrameRate(5);//一秒5针
			param.setPictureFormat(PixelFormat.JPEG);//图片形式
			param.set("jpen-quality",80);//图片质量
			
			MyCameraDemo.this.camera.setParameters(param);
			try {
				MyCameraDemo.this.camera.setPreviewDisplay(MyCameraDemo.this.holder);//设置预览
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			MyCameraDemo.this.camera.startPreview();//进行预览的功能
			MyCameraDemo.this.previewRunTime=true;//已经开始预览
			
		}

		//@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			if(MyCameraDemo.this.camera!=null){//摄像头资源没释放
				if(MyCameraDemo.this.previewRunTime){
					MyCameraDemo.this.camera.stopPreview();//停止预览
					MyCameraDemo.this.previewRunTime=false;
				}
				MyCameraDemo.this.camera.release();//释放摄像头资源
			}
		}
		
	}
	
	/**
	 * 只有对焦成功才能进行拍照并保存
	 * @author yuhang
	 *
	 */
	private class MyAutoFocusCallback implements AutoFocusCallback{

		@Override
		public void onAutoFocus(boolean success, Camera camera) {
			// TODO Auto-generated method stub
			if(success){
			//Callback interface used to signal the moment of actual image capture.
				MyCameraDemo.this.camera.takePicture(shutter, pc, jpgcall);
			}
		}
		
	}
	
	/**
	 * 照完照片后的处理操作.进行图像的保存
	 */
	
	private Camera.PictureCallback jpgcall=new PictureCallback() {
		
		@Override//保存图片。把byte数组中的内容变成Bitmap图片。jpeg
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length);
			/**
			 * 图片的自动命名
			 */
			String fileName=Environment.getExternalStorageDirectory().toString()
					+File.separator+"mlphoto"+File.separator+"ML_"+
					System.currentTimeMillis()+".jpg";
			File file=new File(fileName);
			
			//如果file这个文件夹不存在,新建
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdir();//创建文件夹
				
			}
			/**
			 * Most clients will use output streams that write data to the file system (FileOutputStream), 
			 * the network (getOutputStream()/getOutputStream()), or to an in-memory byte array
			 * (ByteArrayOutputStream).
			 * 
			 * Most clients should wrap their output stream with BufferedOutputStream. 
			 */
			try {
				BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
				bmp.compress(Bitmap.CompressFormat.JPEG, 80, bos);//向缓冲区之中压缩图片
				bos.flush();
				bos.close();
				Toast.makeText(MyCameraDemo.this, "拍照成功已保存在"+fileName+"文件中", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//为下一次拍照准备
			MyCameraDemo.this.camera.stopPreview();
			MyCameraDemo.this.camera.startPreview();
			
		}
	};
	
	private Camera.ShutterCallback shutter=new ShutterCallback() {
		
		//@Override
		public void onShutter() {
			// TODO Auto-generated method stub
			//按下快门之后进行的操作
		}
	};
	
	private Camera.PictureCallback pc=new PictureCallback() {
		
		//@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			
		}
	};
}


Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />

	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".MyCameraDemo" android:label="@string/app_name"
			android:screenOrientation="landscape">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
	<uses-feature android:name="android.hardware.camera" />
	<uses-feature android:name="android.hardware.camera.autofocus" />
	<uses-permission android:name="android.permission.CAMERA" />
	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button
		android:id="@+id/but" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="照相" />
	<SurfaceView
		android:id="@+id/surface"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent" /> 
	
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值