一、创建文件
1.创建一个新的工程, 这里的详细过程就略过了.具体请看参考中新建一个空的app部分
二、添加对应布局
修改布局, 使用FrameLayout来显示预览画面
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="1"/>
这个布局之后会用来显示相机的预览画面, 可以看出这里的画面占据了整个界面
三、定义Preview类
想让预览显示出来,自定义一个View算是一个比较方便的办法, 这里取名为CameraPreview 直接上代码
package 注意这里是你自己的包名;
import android.content.Context;
import android.hardware.Camera; 注意这里的包名不要弄错
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = CameraPreview.class.getSimpleName();
private SurfaceHolder surfaceHolder;
private Camera camera;
public CameraPreview(Context context) {
super(context);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
}
private static Camera getCameraInstance() {
Camera tempCamera = null;
try{
tempCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
} catch (Exception e) {
Log.d(TAG, "camera open failed" + e.getMessage());
}
return tempCamera;
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
if (holder == null) {
return;
}
this.surfaceHolder = holder;
camera = getCameraInstance();
try {
camera.setPreviewDisplay(surfaceHolder);
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Setting preview ERROR " + e.getMessage());
}
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
this.surfaceHolder = surfaceHolder;
startPreview(false);
}
private void startPreview(boolean reuse) {
if (camera != null && surfaceHolder != null) {
try {
if (!reuse) {
camera.setPreviewDisplay(surfaceHolder);
}
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
surfaceHolder.removeCallback(this);
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
}
三、将Preview添加到FrameLayout
直接上代码
package b包名包名;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class CameraMain extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_main);
getPermission();
CameraPreview cameraPreview = new CameraPreview(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cameraPreview);
}
private void getPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
}
}
}
四、在Manifest文件中声明相机权限
权限要写在<manifest>这里边</manifest>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
最后
至此一个最最简陋的相机Demo就做完了,但是他也还不具备任何功能,更多的开发教程之后会更新.
项目源码 参考文章
|