Android 照相功能

下面是一个基于Android2.3.3开发的照相小功能

Layout: activity_main.xml

Android 照相功能
<FrameLayout 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=".MainActivity" >

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/surfaceView" />
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:id="@+id/photoControl">
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/takePhoto"
            android:text="@string/taks_photo"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:onClick="takePhoto"/>
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/autoFocus"
            android:text="@string/auto_focus"
            android:layout_toLeftOf="@id/takePhoto"
            android:layout_alignBottom="@id/takePhoto"
            android:layout_marginRight="5dp"
            android:onClick="takePhoto"/>
    </RelativeLayout>

</FrameLayout>
Android 照相功能

MainActivity.java

Android 照相功能
package com.lk.takephoto;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.app.Activity;

public class MainActivity extends Activity {
    private RelativeLayout photoControl;
    private SurfaceView surfaceView;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //设置无标题,全屏
        //1. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        //2. 
        requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        
        setContentView(R.layout.activity_main);
        
        photoControl = (RelativeLayout) findViewById(R.id.photoControl);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.setKeepScreenOn(true);
        surfaceHolder.setFixedSize(176, 144);
        surfaceHolder.addCallback(new SurfaceHolderCallback());
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //点击屏幕
            if(photoControl.getVisibility() == View.GONE) {
                photoControl.setVisibility(View.VISIBLE);
            } else {
                photoControl.setVisibility(View.GONE);
            }
        }
        return super.onTouchEvent(event);
    }
    
    public void takePhoto(View view) {
        switch (view.getId()) {
        case R.id.takePhoto:
            camera.takePicture(null, null, new MyPictureCallback());
            break;
        case R.id.autoFocus:
            camera.autoFocus(null);
            break;
        }
    }
    
    private final class SurfaceHolderCallback implements SurfaceHolder.Callback {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                camera = Camera.open();  //打开摄像头
                Camera.Parameters parameters = camera.getParameters();
//                Log.i("tag", parameters.flatten());  //打印camera 参数
                parameters.setPreviewSize(320, 240);
                camera.setParameters(parameters);
                camera.setPreviewDisplay(surfaceView.getHolder());  //设置输出到哪
                camera.startPreview();  //开始预览
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            //释放资源
            camera.release();
            camera = null;
        }
        
    }
    
    private final class MyPictureCallback implements PictureCallback {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream fos = null;
            try {
                String fileName = System.currentTimeMillis() + ".jpg";
                File file = new File(Environment.getExternalStorageDirectory(), fileName );
                fos = new FileOutputStream(file);
                fos.write(data);
                camera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
    }

}
Android 照相功能

AndroidManifest.xml

Android 照相功能
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lk.takephoto"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />
    
    <!-- 拍照的权限 -->
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

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

</manifest>
Android 照相功能

 

strings.xml

Android 照相功能
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">拍照</string>
    <string name="action_settings">Settings</string>
    <string name="taks_photo">拍照</string>
    <string name="auto_focus">聚焦</string>

</resources>
Android 照相功能

 

 

Android 照相功能,布布扣,bubuko.com

Android 照相功能

上一篇:canvas玩转微信红包


下一篇:Android之Android apk动态加载机制的研究(二):资源加载和activity生命周期管理