android 图片预览切换,Android 实例实现自定义Camera和前后置摄像头切换以及图片缩小放大预览...
时间: 2023-12-06 20:05:12 浏览: 175
好的,我会尽力回答你的问题。首先,让我们来看一下如何实现自定义Camera和前后置摄像头切换。
1. 自定义Camera
首先,在AndroidManifest.xml文件中添加以下权限:
```
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
```
然后,在你的Activity中,创建一个Preview类,继承SurfaceView,并实现SurfaceHolder.Callback接口。
```
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public Preview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("Preview", "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mHolder.getSurface() == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
Log.d("Preview", "Error stopping camera preview: " + e.getMessage());
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d("Preview", "Error starting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mHolder.removeCallback(this);
}
}
```
然后,在你的Activity中,创建一个Camera对象,并将其设置给Preview类。
```
public class CameraActivity extends Activity {
private Camera mCamera;
private Preview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCamera = getCameraInstance();
mPreview = new Preview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
Log.d("CameraActivity", "Error opening camera: " + e.getMessage());
}
return c;
}
}
```
2. 前后置摄像头切换
为了实现前后置摄像头的切换,你需要先判断设备是否支持前置摄像头。如果支持,你可以通过Camera.getNumberOfCameras()方法来获取设备上可用的摄像头数量。然后,你可以通过Camera.open()方法打开指定的摄像头。
```
public void switchCamera() {
if (Camera.getNumberOfCameras() > 1) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
if (currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
} else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
mCamera = Camera.open(currentCameraId);
mPreview.setCamera(mCamera);
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
Log.d("CameraActivity", "Error setting camera preview: " + e.getMessage());
}
mCamera.startPreview();
}
}
```
3. 图片缩小放大预览
为了实现图片的缩小和放大预览,你可以使用Matrix类来对Bitmap进行缩放操作。
```
public void zoom(float scale) {
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
mImageView.setImageBitmap(scaledBitmap);
}
```
这样,你就可以实现自定义Camera和前后置摄像头切换以及图片缩小放大预览了。
阅读全文