Android (拍照功能)

拍照功能和视频播放用到了一个共同的控件SurfaceView


这一次我们要在XML中配置的东西稍微有点多

    
    
    
    

然后是布局文件



    

    

再是java部分

package com.example.myas_028;

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

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

/**
 * Created by Administrator on 2017/7/21 0021.
 */

public class CameraActivity extends AppCompatActivity {

    private SurfaceView sv_camera_view;
    private Camera camera;

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

        sv_camera_view.getHolder().addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //打开摄像头
                camera = Camera.open();
                //设置摄像参数
                Camera.Parameters parameters=camera.getParameters();
                parameters.setPictureFormat(PixelFormat.JPEG);

                parameters.set("jpeg-quality",100);
                camera.setParameters(parameters);

                //开启预览效果
                camera.startPreview();
                //将画面展示到surfaceView
                try {
                    camera.setPreviewDisplay(sv_camera_view.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                camera.startPreview();
                camera.release();
            }
        });
    }

    public void takePhoto(View view){
        camera.takePicture(null,null,new Camera.PictureCallback(){
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
               Bitmap bitmap=BitmapFactory.decodeByteArray(data,0,data.length);

               //时间戳
                Calendar calendar=Calendar.getInstance();
                int year=calendar.get(Calendar.YEAR);
                int month=calendar.get(Calendar.MONTH);
                int day=calendar.get(Calendar.DAY_OF_MONTH);

                try {
                    FileOutputStream fos=new FileOutputStream("/mnt/sdcard/dcim/camera/G160628_"+year+(month+1)+day+"_"+System.currentTimeMillis()+".jsp");
                    //压缩
                    bitmap.compress(Bitmap.CompressFormat.JPEG,85,fos);

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



你可能感兴趣的:(Android (拍照功能))