使用Camera进行拍照 & 后台静默拍照的代码
时间: 2024-02-24 17:55:18 浏览: 97
使用Camera进行拍照的代码:
```java
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final String TAG = "CameraActivity";
private Camera mCamera;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mSurfaceView = findViewById(R.id.surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e(TAG, "Error setting preview display", e);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mCamera.startPreview();
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// 判断是否需要拍照
if (needTakePicture()) {
// 拍照
mCamera.takePicture(null, null, mPictureCallback);
}
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 保存照片到指定目录
savePicture(data);
camera.startPreview();
}
};
private boolean needTakePicture() {
// 判断是否需要拍照的逻辑
return false;
}
private void savePicture(byte[] data) {
// 保存照片到指定目录的逻辑
}
}
```
后台静默拍照的代码:
```java
public class CameraService extends Service implements SurfaceHolder.Callback {
private static final String TAG = "CameraService";
private Camera mCamera;
private SurfaceHolder mSurfaceHolder;
@Override
public void onCreate() {
super.onCreate();
// 初始化SurfaceHolder
SurfaceView surfaceView = new SurfaceView(this);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
1, 1,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(surfaceView, layoutParams);
mSurfaceHolder = surfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
@Override
public void onDestroy() {
super.onDestroy();
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e(TAG, "Error setting preview display", e);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mCamera.startPreview();
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// 判断是否需要拍照
if (needTakePicture()) {
// 拍照
mCamera.takePicture(null, null, mPictureCallback);
}
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 保存照片到指定目录
savePicture(data);
camera.startPreview();
}
};
private boolean needTakePicture() {
// 判断是否需要拍照的逻辑
return false;
}
private void savePicture(byte[] data) {
// 保存照片到指定目录的逻辑
}
}
```
需要注意的是,在使用后台静默拍照的方法时,需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
```
阅读全文