android自定义相机拉伸surfaceview
Posted
技术标签:
【中文标题】android自定义相机拉伸surfaceview【英文标题】:android custom camera stretched surfaceview 【发布时间】:2016-03-30 18:42:25 【问题描述】:我遇到了摄像头视图的问题,即摄像头的视图被拉伸,并且不像默认的摄像头应用程序或任何其他摄像头应用程序提供的正常视图。 我一直无法获得相机用来拉伸大于屏幕尺寸的表面视图。
enter code here
private class SurfaceCallback implements SurfaceHolder.Callback
@Override
public void surfaceCreated(SurfaceHolder holder)
try
camera = Camera.open();
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(getPreviewDegree(MainActivity.this));
camera.startPreview(); //
catch (Exception e)
e.printStackTrace();
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
setCameraParameters();
@Override
public void surfaceDestroyed(SurfaceHolder holder)
private int getPreviewDegree(Activity activity)
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degree = 0;
switch (rotation)
case Surface.ROTATION_0:
degree = 90;
break;
case Surface.ROTATION_90:
degree = 0;
break;
case Surface.ROTATION_180:
degree = 270;
break;
case Surface.ROTATION_270:
degree = 180;
break;
return degree;
private void setCameraParameters()
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();
if (sizeList.size() > 0)
Camera.Size cameraSize = sizeList.get(0);
parameters.setPreviewSize(cameraSize.width, cameraSize.height);
sizeList = parameters.getSupportedPictureSizes();
if (sizeList.size() > 0)
Camera.Size cameraSize = sizeList.get(0);
for (Camera.Size size : sizeList)
if (size.width * size.height < 10 * 100)
cameraSize = size;
break;
parameters.setPictureSize(cameraSize.width, cameraSize.height);
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setJpegQuality(100);
parameters.setJpegThumbnailQuality(100);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(parameters);
startOrientationChangeListener();
private void startOrientationChangeListener()
OrientationEventListener mOrEventListener = new OrientationEventListener(MainActivity.this)
@Override
public void onOrientationChanged(int rotation)
if (((rotation >= 0) && (rotation <= 45)) || (rotation > 315))
rotation = 0;
else if ((rotation > 45) && (rotation <= 135))
rotation = 90;
else if ((rotation > 135) && (rotation <= 225))
rotation = 180;
else if ((rotation > 225) && (rotation <= 315))
rotation = 270;
else
rotation = 0;
if (rotation == mOrientation)
return;
mOrientation = rotation;
updateCameraOrientation();
;
mOrEventListener.enable();
private void updateCameraOrientation()
if (camera != null)
Camera.Parameters parameters = camera.getParameters();
int rotation = 90 + mOrientation == 360 ? 0 : 90 + mOrientation;
if (mIsFrontCamera)
if (rotation == 90) rotation = 270;
else if (rotation == 270) rotation = 90;
parameters.setRotation(rotation);
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
【问题讨论】:
【参考方案1】:您可以从这个项目开始。https://github.com/pikanji/CameraPreviewSample
【讨论】:
【参考方案2】:在 SurfaceCallback 类的 surfaceCreated 方法中添加 SurfaceHoler 参数后尝试:
holder = mSurfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
希望对你有所帮助。
【讨论】:
您可以参考下面的链接了解更多详情。 ***.com/questions/18545889/…以上是关于android自定义相机拉伸surfaceview的主要内容,如果未能解决你的问题,请参考以下文章
使SurfaceView大于屏幕(将相机预览适配到大于显示的SurfaceView)
使SurfaceView大于屏幕(将相机预览适配到大于显示的SurfaceView)