Android的Camera用SurfaceView拍照

首先必须考虑Camera的Parameter的设置,必须要设置他的预览大小和图片大小。默认是用最大的,有时候会导致OOM错误。所以我们得算出最优的Size。

<pre name="code" class="java">
Camera.Parameters mParameters = mCamera.getParameters();List<Camera.Size> allSizes = mParameters.getSupportedPictureSizes(); List<Camera.Size> allSizes = mParameters.getSupportedPictureSizes(); List<Camera.Size> previewSizes = mParameters.getSupportedPreviewSizes(); Camera.Parameters mParameters = mCamera.getParameters();List<Camera.Size> allSizes = mParameters.getSupportedPictureSizes(); List<Camera.Size> previewSizes = mParameters.getSupportedPreviewSizes(); //1 PictureSizes和PreviewSizes都支持 2宽度小于屏幕宽度 List<Camera.Size> commonSizes=new ArrayList<>(); int previewWidth = Utils.getDeviceWidth(getActivity()); for(Camera.Size pictureSize:allSizes){ if(pictureSize.width<previewWidth){ for(Camera.Size previewSize:allSizes){ if(previewSize.width<previewWidth && pictureSize.width==previewSize.width && pictureSize.height==previewSize.height){ commonSizes.add(pictureSize); } } } } Collections.sort(commonSizes, new Comparator<Camera.Size>() { @Override public int compare(Camera.Size lhs, Camera.Size rhs) { return rhs.width*rhs.height-lhs.height*lhs.width; } }); Camera.Size size = commonSizes.get(0); // get top size //set max Picture Size,这两个最好是用同一个size,否则拍出来的图片跟预览的有差异或者不同 mParameters.setPictureSize(size.width, size.height);//设置最终得到图片的大小 mParameters.setPreviewSize(size.width, size.height);//设置预览图片的大小mCamera.setDisplayOrientation(90);//必须设置90否则预览会出现 预览窗口旋转了n*90°,不同厂商旋转的角度可能会不一样。Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info);result=info.orientation;//Camera.Parameters mParameters = mCamera.getParameters();List<Camera.Size> allSizes = mParameters.getSupportedPictureSizes(); List<Camera.Size> allSizes = mParameters.getSupportedPictureSizes(); List<Camera.Size> previewSizes = mParameters.getSupportedPreviewSizes(); Camera.Parameters mParameters = mCamera.getParameters();List<Camera.Size> allSizes = mParameters.getSupportedPictureSizes(); List<Camera.Size> previewSizes = mParameters.getSupportedPreviewSizes(); //1 PictureSizes和PreviewSizes都支持 2宽度小于屏幕宽度 List<Camera.Size> commonSizes=new ArrayList<>(); int previewWidth = Utils.getDeviceWidth(getActivity()); for(Camera.Size pictureSize:allSizes){ if(pictureSize.width<previewWidth){ for(Camera.Size previewSize:allSizes){ if(previewSize.width<previewWidth && pictureSize.width==previewSize.width && pictureSize.height==previewSize.height){ commonSizes.add(pictureSize); } } } } Collections.sort(commonSizes, new Comparator<Camera.Size>() { @Override public int compare(Camera.Size lhs, Camera.Size rhs) { return rhs.width*rhs.height-lhs.height*lhs.width; } }); Camera.Size size = commonSizes.get(0); // get top size //set max Picture Size,这两个最好是用同一个size,否则拍出来的图片跟预览的有差异或者不同 mParameters.setPictureSize(size.width, size.height);//设置最终得到图片的大小 mParameters.setPreviewSize(size.width, size.height);//设置预览图片的大小mCamera.setDisplayOrientation(90);//必须设置90否则预览会出现 预览窗口旋转了n*90°,不同厂商旋转的角度可能会不一样。Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info);result=info.orientation;//在PictrueCallback中会用来把图片旋转过来

接下来就是回调方法了
Camera.PictureCallback picture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            
Matrix matrix = new Matrix();
matrix.postRotate(mRotateDegree);// 旋转45度 == matrix.setSinCos(0.5f, 0.5f);
newBitmap = Bitmap.createBitmap(rotateBitmap, 0, 0, rotateBitmap.getWidth(),
        rotateBitmap.getHeight(), matrix, true);

        }
    };

阅读更多
上一篇Android 的notification 的intent 传值
下一篇android调用系统的相机拍照 、裁剪,然后保存
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭