Android系列之照相功能的实现

xml配置文件:(一定要写上,不然那会有bug的)

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

xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zking.laci.android28_vedio.Main3Activity">
    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_main3"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拍照"
        android:onClick="takePhoto"
        />

</RelativeLayout>

java代码:

package com.zking.laci.android28_vedio;

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 android.widget.Toast;

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

public class Main3Activity extends AppCompatActivity {

    private SurfaceView sv_main3;
    private Camera camera;

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

            sv_main3= (SurfaceView) findViewById(R.id.sv_main3);
            sv_main3.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_main3.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.stopPreview();
                    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()+".jpg");
                    //压缩
                    bitmap.compress(Bitmap.CompressFormat.JPEG,85,fos);

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

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

            }
        });
    }
}






















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值