package com.zzk.a1508camera;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
File picFile;
Uri picUri;
SurfaceView preview;
Camera mCamera;
int cameraID = 0;
boolean isPreviewing = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
LinearLayout line1 = new LinearLayout(this);
line1.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(line1);
LinearLayout line2 = new LinearLayout(this);
line2.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(line2);
picFile = new File(getExternalFilesDir(null), "picture.jpg");
// 定义多媒体文件的uri,在应用之间传递文件时需要用uri
if(Build.VERSION.SDK_INT>=24){ // Android 7 以后不能直接用file uri分享文件,要使用FileProvider
String fileProviderAuthority = getPackageName() + ".fileProvider"; // FileProvider的名字,FileProvider在应用配置文件AndroidManifest中定义
// 格式为:content://com.zzk.a1508camera.fileProvider/testdir/picture.jpg, testdir是res/file_paths/file_paths.xml中定义的目录别名
picUri = FileProvider.getUriForFile(MainActivity.this, fileProviderAuthority, picFile);
} else { // Android 7 以前可以直接用file uri分享文件
// 格式为:file:///storage/emulated/0/Android/data/com.zzk.a1508camera/files/picture.jpg
picUri = Uri.fromFile(picFile);
}
Button btnOpen = new Button(this);
btnOpen.setText("Open");
line1.addView(btnOpen);
btnOpen.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 101);
return;
}
open(cameraID);
}
});
Button btnStartPreview = new Button(this);
btnStartPreview.setText("Start Preview");
line1.addView(btnStartPreview);
btnStartPreview.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(mCamera!=null) {
mCamera.startPreview();
isPreviewing = true;
}
}
});
Button btnTake = new Button(this);
btnTake.setText("Take Photo");
line1.addView(btnTake);
btnTake.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
mCamera.takePicture(null, null, new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
FileOutputStream fos;
try {
fos = new FileOutputStream(picFile);
fos.write(arg0);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
isPreviewing = false;
}
});
Button btnStopPreview = new Button(this);
btnStopPreview.setText("Stop Preview");
line2.addView(btnStopPreview);
btnStopPreview.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(mCamera!=null) {
mCamera.stopPreview();
isPreviewing = false;
}
}
});
Button btnRelease = new Button(this);
btnRelease.setText("Release");
line2.addView(btnRelease);
btnRelease.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(mCamera!=null) {
mCamera.release();
isPreviewing = false;
}
}
});
Button btnView = new Button(this);
btnView.setText("View");
line2.addView(btnView);
btnView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(picFile.exists()){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(picUri, "image/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 授予对方读取该文件的权限
startActivity(intent);
}
}
});
Button btnSwitch = new Button(this);
btnSwitch.setText("Switch Camera");
ll.addView(btnSwitch);
btnSwitch.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(isPreviewing){
cameraID++;
if(cameraID>=Camera.getNumberOfCameras()) cameraID = 0;
mCamera.stopPreview();
mCamera.release();
open(cameraID);
mCamera.startPreview();
}
}
});
preview = new SurfaceView(this);
ll.addView(preview);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode==101) {
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
open(cameraID);
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
void open(int id){
mCamera = Camera.open(id);
Camera.Parameters param = mCamera.getParameters();
//param.setPictureFormat(PixelFormat.JPEG);
//param.setPictureSize(1024, 768);
mCamera.setParameters(param);
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(preview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "Camera opened", Toast.LENGTH_SHORT).show();
}
}