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();
}
}
});
}
}