package com.example.sj.mycamera;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
import java.util.List;
public class MainActivity extends Activity implements SurfaceHolder.Callback, Camera.PictureCallback {
Camera mCamera;
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
private int mCurrentPos = 0;
ImageView mImageView;
boolean hasAutoFocus=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mImageView= (ImageView) this.findViewById(R.id.imgv);
mSurfaceView = (SurfaceView) this.findViewById(R.id.sv);
mSurfaceHolder = mSurfaceView.getHolder();
//2.+
//mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.addCallback(this);
}
private void startPreView() {
int numberOfCameras = Camera.getNumberOfCameras();
System.out.println("numberOfCameras = " + numberOfCameras);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
//把指定索引位置摄像头信息封装到cameraInfo对象中
Camera.getCameraInfo(i, cameraInfo);
if (i==mCurrentPos&&cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
mCamera=Camera.open(i);
}
if (i==mCurrentPos&&cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
System.out.println("i--->"+i);
mCamera=Camera.open(i);
}
}
mCamera.setDisplayOrientation(getCameraDegrees());
Camera.Parameters parameters = mCamera.getParameters();
//获取全部的预览尺寸
List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
System.out.println("预览尺寸列表:");
for (Camera.Size size : supportedPreviewSizes) {
System.out.println("宽: " + size.width + " 高: " + size.height);
if (size.width*1.0f/size.height==16*1.0f/9){
Log.e("xx", "预览选择:---》"+size.width+" "+size.height );
parameters.setPreviewSize(size.width, size.height);
break;
}
}
System.out.println("预览尺寸列表完毕");
//获取全部最终成相尺寸
List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();
System.out.println("最终成相尺寸列表:");
for (Camera.Size size : supportedPictureSizes) {
System.out.println("宽: " + size.width + " 高: " + size.height);
if (size.width*1.0f/size.height==16*1.0f/9){
Log.e("xx", "最终成相选择:---》"+size.width+" "+size.height );
parameters.setPictureSize(size.width, size.height);
break;
}
}
System.out.println("最终成相尺寸列表完毕");
if (mCurrentPos == 0) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
mCamera.setParameters(parameters);
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
public int getCameraDegrees() {
int degress = 0;
switch (this.getWindowManager().getDefaultDisplay().getRotation()) {
case Surface.ROTATION_0:
degress = 90;
break;
case Surface.ROTATION_90:
degress = 0;
break;
case Surface.ROTATION_180:
degress = 270;
break;
case Surface.ROTATION_270:
degress = 180;
break;
}
return degress;
}
public void resetCamara() {
if (mCamera == null) {
return;
}
mCamera.stopPreview();
mCamera.release();
}
public void 切换(View v) {
resetCamara();
if (mCurrentPos == 0) {
mCurrentPos = 1;
} else {
mCurrentPos = 0;
}
startPreView();
}
public void 拍照(View v) {
hasAutoFocus=false;
if (mCurrentPos==1){
mCamera.takePicture(null,null,this);
}else {
try {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
//自动对焦成功,只执行一次拍摄
if (!hasAutoFocus){
hasAutoFocus=true;
mCamera.takePicture(null,null,MainActivity.this);
}
}
});
} catch (Exception e) {
mCamera.takePicture(null,null,MainActivity.this);
}
}
}
public void 重拍(View v){
mImageView.setVisibility(View.GONE);
startPreView();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//初次创建---第一次能看见
startPreView();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//状态变化
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//最终销毁---看不见了
resetCamara();
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
resetCamara();
Bitmap bitmapOrigin= BitmapFactory.decodeByteArray(data,0,data.length);
Matrix matrix=new Matrix();
if (mCurrentPos==0){
matrix.postRotate(90);
}else {
matrix.postRotate(-90);
matrix.postScale(-1, 1);
}
Bitmap bitmapUse=Bitmap.createBitmap(bitmapOrigin,0,0,bitmapOrigin.getWidth(),bitmapOrigin.getHeight(),matrix,false);
mImageView.setImageBitmap(bitmapUse);
mImageView.setVisibility(View.VISIBLE);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.sj.mycamera.MainActivity">
<SurfaceView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ImageView
android:id="@+id/imgv"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:text="切换"
android:onClick="切换"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="X"
android:onClick="重拍"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="拍照"
android:onClick="拍照"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
|