使用Camera SurfaceView预览出现图案形变等问题解决总结

测试Camera使用SurfaceView预览,出现,图像拉伸,以及图案旋转90°,保存下的图片同样有以上问题。解决:

 android.hardware.Camera.Parameters parameters = camera.getParameters();
                    android.hardware.Camera.Size bestSize = null;

                    List.hardware.Camera.Size> sizeList = camera.getParameters().getSupportedPreviewSizes();
                    bestSize = sizeList.get(0);

                    for(int i = 1; i < sizeList.size(); i++){
                        if((sizeList.get(i).width * sizeList.get(i).height) >
                                (bestSize.width * bestSize.height)){
                            bestSize = sizeList.get(i);//获取最佳尺寸
                        }
                    }

                    parameters.setPreviewSize(bestSize.width, bestSize.height);
                    camera.setParameters(parameters);
                    camera.setDisplayOrientation(90);//解决图像旋转90度问题
                    camera.setParameters(parameters);
                    camera.startPreview();

图片保存之前进行以下处理:

 Bitmap bm0 = BitmapFactory.decodeByteArray(data, 0, data.length);
                    Matrix m = new Matrix();
                    m.setRotate(90, (float) bm0.getWidth() / 2, (float) bm0.getHeight() / 2);
                    final Bitmap bm = Bitmap.createBitmap(bm0, 0, 0, bm0.getWidth(), bm0.getHeight(), m, true);
                    out = new FileOutputStream(fileName);
                    bm.compress(Bitmap.CompressFormat.PNG, 90, out);
                    out.flush();

你可能感兴趣的:(图片处理)