本文主要是介绍Android Camera照相机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java代码:- 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 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);
- }
- }
- <?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>
转自:http://blog.csdn.net/tzqncwu/article/details/6682820
这篇关于Android Camera照相机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!