关闭

Android的Camera用SurfaceView拍照

60人阅读 评论(0) 收藏 举报

首先必须考虑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);

        }
    };

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:812次
    • 积分:84
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档