我在Android> = 4.1.2上实现了相机预览。在Galaxy S4上的Android 5上(两个不同的设备)视图显示三个摄像头预览和绿色屏幕:
在Galaxy S3(Android 4.3),Galaxy S2(Android 4.1.2)和Galaxy Tab S(Android 5.0.1)上,它可以正常工作。
这是我的代码:
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.List;
@SuppressWarnings("deprecation")
public class CameraView extends SurfaceView {
private boolean inPreview = false;
private boolean surfaceCreated = false;
private Camera camera = null;
private Camera.Size previewSize = null;
public CameraView(Context context) {
super(context);
SurfaceHolder surfaceHolder = getHolder();
SurfaceHolder.Callback surfaceHolderCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
surfaceCreated = true;
try {
camera = Camera.open();
camera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
camera.release();
camera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (holder.getSurface() == null || camera == null) {
return;
}
camera.stopPreview();
List<Camera.Size> sizes = camera.getParameters().getSupportedPictureSizes();
previewSize = getOptimalPreviewSize(sizes, width, height);
startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
surfaceCreated = false;
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
};
surfaceHolder.addCallback(surfaceHolderCallback);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void onResume() {
if(!inPreview && camera != null && surfaceCreated) {
startPreview();
}
}
public void onPause() {
if(camera != null) {
camera.stopPreview();
inPreview = false;
}
}
private void startPreview() {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
camera.startPreview();
inPreview = true;
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - h) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - h);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - h) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - h);
}
}
}
return optimalSize;
}
}
用法:
public class MainActivity extends Activity {
private CameraView cameraView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cameraView = new CameraView(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
cameraView.setLayoutParams(params);
setContentView(cameraView);
}
@Override
protected void onResume() {
super.onResume();
cameraView.onResume();
}
@Override
protected void onPause() {
super.onPause();
cameraView.onPause();
}
}
我只支持横向模式。