大佬教程收集整理的这篇文章主要介绍了android – 我/艺术:找不到类:,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
实际上我正在开发Android Camera API应用程序,问题是我的Front Facing Camera Preview是Stretch.它只发生在华为p8 lite等2-3个设备上.当切换到前置摄像头时,设备会拉伸摄像头预览!
我有相机预览类:
public class CameraPreview extends SurfaceView implements SurfaceHolder.CallBACk { //Variables de' Preview private SurfaceHolder mHolder; private List<Camera.Size> mSupportedPreviewSizes; private Camera.Size mPreviewSize; privatE int FOCUS_AREA_SIZE=300; public static Camera previewCamera; //////////////////////// //protected LOGGER keys protected final String EXCEPTION_KEY="xception"; ////////////////////////// public CameraPreview(Context context,Camera camera) { super(context); //get the camera previewCamera = camera; if(previewCamera!=null){ // supported preview sizes mSupportedPreviewSizes = previewCamera.getParameters().getSupportedPreviewSizes(); } else{ previewCamera=camera; // supported preview sizes mSupportedPreviewSizes = previewCamera.getParameters().getSupportedPreviewSizes(); } // Install a SurfaceHolder.CallBACk so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallBACk(this); // deprecated setTing,but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); setKeepScreenOn(true); setPreview(); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { try{ //when the surface is created,we can set the camera to draw images in this surfaceholder setPreview(); previewCamera.setPreviewDisplay(surfaceHolder); previewCamera.startPreview(); if(CameraActivity.cameraId==1){ previewCamera.setFaceDetectionListener(faceDetectionListener); previewCamera.startFaceDetection(); } } catch(Exception exp){ Log.i(EXCEPTION_KEY,"FROM surfaceCreated: "+exp.toString()); } } @Override public void surfaceChanged(SurfaceHolder surfaceHolder,int i,int i2,int i3) { //before changing the application orientation,you need to stop the preview,rotate @R_674_6296@en start it again if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data return; try{ previewCamera.stopPreview(); } catch (Exception E){ //this will happen when you are trying the camera if it's not running } //Now,recreate the camera preview try{ //set the camera preview on every preview change setPreview(); //in case the preview is still stretched bingo! Then... // Camera.Parameters params = previewCamera.getParameters(); // List<Camera.Size> sizes = params.getSupportedPreviewSizes(); // Camera.Size optimalSize = PreviewSizeParameters.getOptimalPreviewSize(sizes,i2,i3); // params.setPreviewSize(optimalSize.width,optimalSize.height); // previewCamera.setParameters(params); previewCamera.setPreviewDisplay(mHolder); previewCamera.startPreview(); if(CameraActivity.cameraId==1){ previewCamera.setFaceDetectionListener(faceDetectionListener); previewCamera.startFaceDetection(); } } catch(Exception exp){ Log.i(EXCEPTION_KEY,"FROM surfaceChanged: "+exp.toString()); } } public void setPreview(){ try{ //set the focusable true this.setFocusable(true); //set the touch able true this.setFocusableInTouchMode(true); //set the camera display orientation lock previewCamera.setDisplayOrientation(90); //get the camera parameters Camera.Parameters parameters = previewCamera.getParameters(); //set the preview size parameters.setPreviewSize(mPreviewSize.width,mPreviewSize.height); //set the parameter previewCamera.setParameters(parameters); }catch(Exception exp){ Log.i(EXCEPTION_KEY,"FROM setPreview: "+exp.toString()); } } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { //our app has only one screen,so we'll destroy the camera in the surface //if you are using with more screens,please move this code your activity try{ //handle in Activity onResume and onPause }catch(Exception exp){ Log.i(EXCEPTION_KEY,"FROM surfaceDestroyed: "+exp.toString()); } } //Override Methods here @Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) { try{ final int width = resolveSize(getSuggestedMinimumWidth(),widthMeasureSpec); final int height = resolveSize(getSuggestedMinimumHeight(),heightMeasureSpec); setMeasuredDimension(width,height); if (mSupportedPreviewSizes != null) { mPreviewSize = PreviewSizeParameters.getOptimalPreviewSize(mSupportedPreviewSizes,width,height); } }catch(Exception exp){ Log.i(EXCEPTION_KEY,"FROM onMeasure: "+exp.toString()); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void stopPreviewAndFreeCamera() { if (previewCamera != null) { // Call stopPreview() to stop updating the preview surface. previewCamera.stopPreview(); // Important: Call release() to release the camera for use by other // applications. Applications should release the camera immediately // during onPause() and re-open() it during onResume()). previewCamera.release(); previewCamera = null; } } //end of outer class here }
自从过去几天以来,我一直在解决这个问题,并在我的自定义相机预览中获得相同的拉伸效果.
所以我获得最佳预览尺寸的方法是:
public class PreviewSizeParameters { //protected LOGGER keys protected static final String EXCEPTION_KEY = "xception"; ////////////////////////// public static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes,int w,int h) { final double ASPECT_TOLERANCE = 0.1; double targetRatio = (doublE) h / w; if (sizes == null) return null; Camera.Size optimalSize = null; double minDiff = Double.max_value; int targetHeight = h; 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 - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } if (optimalSize == null) { minDiff = Double.max_value; for (Camera.Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } }
问题
问题是只在前置摄像头上拉伸摄像头预览.我的日志cat数据如下:
它显示了一些行开头:I / art:找不到类:因为我是新手,所以我不知道它们是什么意思?
以上是大佬教程为你收集整理的android – 我/艺术:找不到类:全部内容,希望文章能够帮你解决android – 我/艺术:找不到类:所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。