Android多媒体之相机Camera

这是布局整体预览图
布局预览图

layout布局文件夹中activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/take_b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击拍照" />

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/take_b" />

    <ImageView 
        android:id="@+id/img"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:background="#0000FF"/>

</RelativeLayout>

MainActivity.java中源程序

package com.zhidi.stucamera;

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

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener{
    private Button take_b;
    private ImageView img;
    private SurfaceView surface;
    private Camera camera;
    private String fname;
    private SurfaceHolder holder;
    private final String SDCARD_HOME = Environment.getExternalStorageDirectory()+File.separator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initEvent();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.take_b:
            camera.takePicture(null, null, new PictureCallback() {

                @Override
                public void onPictureTaken(byte[] data, Camera camera) {
                    new MyAsyncTask().execute(data);
                    camera.startPreview();
                }
            });
            break;

        default:
            break;
        }
    }

    class MyCallback implements Callback{

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            //打开摄像头
            camera = Camera.open();
            //设置摄像头预览角度
            camera.setDisplayOrientation(90);
            try {
                //设置摄像头预览显示器
                camera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //开始预览
            camera.startPreview();
        }
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            //获得摄像头参数
            Parameters p = camera.getParameters();
            //设置摄像头参数
            camera.setParameters(p);
            //开始预览
            camera.startPreview();
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            //释放摄像头
            camera.release();
            camera = null;
        }

    }

    //自定义异步保存图片类
    class MyAsyncTask extends AsyncTask<byte[], String, String>{

        @Override
        protected String doInBackground(byte[]... params) {
            byte[] param = params[0];
            fname = SDCARD_HOME+"IMG"+System.currentTimeMillis()+".jpg";
            File file = new File(fname);
            try {
                FileOutputStream out = new FileOutputStream(file);
                out.write(param);
                out.flush();
                out.close();
                return "OK";
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            Drawable drawable = Drawable.createFromPath(fname);
            img.setImageDrawable(drawable);
            super.onPostExecute(result);
        }
    }

    private void init(){
        take_b = (Button) findViewById(R.id.take_b);
        img = (ImageView) findViewById(R.id.img);
        surface = (SurfaceView) findViewById(R.id.surfaceView1);
        holder = surface.getHolder();
    }

    private void initEvent(){
        take_b.setOnClickListener(this);
        holder.addCallback(new MyCallback());
    }
}

在AndroidManifest.xml文件中注册必要的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zhidi.stucamera"
    android:versionCode="1"
    android:versionName="1.0" >

    <!-- 调用摄像头功能 -->
    <uses-feature android:name="android.hardware.camera" />
    <!-- 调用自动对焦功能 -->
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <!-- 设置允许拍照的权限-->
    <uses-permission android:name="android.permission.CAMERA" />
    <!-- SD卡创建与删除文件权限-->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <!-- 配置SD卡权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="20" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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>

</manifest>
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/x1299906945/article/details/51545225
文章标签: android 多媒体 相机
个人分类: Android
上一篇Android欢迎界面
下一篇数据存储之SharedPreferences
想对作者说点什么? 我来说一句

android相机拍照camera

2015年12月24日 159KB 下载

android camera 相机程序

2011年10月24日 123KB 下载

没有更多推荐了,返回首页

关闭
关闭