Android 实现拍照功能

展示效果图:

[这个是拍的相片]
Android 实现拍照功能_第1张图片

CameraActivity.java

package com.example.videodisplay;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

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

public class CameraActivity extends AppCompatActivity {

    private SurfaceView sfv;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        sfv = (SurfaceView) findViewById(R.id.sv_camera_surfaceview);

        sfv.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                //打开照相机
                camera = Camera.open();
                //给照相机设置参数
                Camera.Parameters parameters= camera.getParameters();
                //设置保存的格式
                parameters.setPictureFormat(PixelFormat.JPEG);
                parameters.set("jpeg-quality",85);
                camera.setParameters(parameters);
                try {
                    camera.setPreviewDisplay(sfv.getHolder());
                    //开启预览
                    camera.startPreview();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            }
            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                //关闭
            }
        });
    }

    public void takePhoto(View view){
        //拍照
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] bytes, Camera camera) {
                //将字节数组转成图片
                Bitmap bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                try {
                    FileOutputStream fos=new FileOutputStream("/mnt/sdcard/360Video/Camera_"+System.currentTimeMillis()+".png");
                    bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);

                    camera.stopPreview();
                    camera.startPreview();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}

activity_camera.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.videodisplay.CameraActivity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_camera_surfaceview"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拍照"
        android:onClick="takePhoto"
        />
RelativeLayout>

最后的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你可能感兴趣的:(Android)