android - 相机预览始终返回 320x240 位图

标签 android bitmap android-camera

我正在创建图像预览,我希望获得非常高的图片分辨率。然而,我不知道为什么,尽管 Galaxy Nexus 支持多种分辨率(我认为最大的是 1920x1080,但我不记得这些是否是确切的值),当我保存图片时,它说来自预览的信息是 320x240。

pixels.mCamera = getCameraInstance();
Camera.Parameters params = mCamera.getParameters();
List<Size> sizes = params.getSupportedPreviewSizes();
Size biggestSize = sizes.get(0);
double biggestPixels = biggestSize.width + biggestSize.height;
for (Size size : sizes) {
    double pixels = size.width * size.height;
    if (pixels > biggestPixels) {
        biggestSize = size;
        biggestPixels = pixels;
    }
}
// At this point, biggestSize is 1920x1080
float ratio = ((float) biggestSize.width)
                    / ((float) biggestSize.height);
params.setPreviewSize(biggestSize.width, biggestSize.height);
mCamera.setParameters(params);
setCameraDisplayOrientation(this, 0, mCamera);
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
LinearLayout innerWrapper = (LinearLayout) findViewById(R.id.innerWrapper);
LayoutParams layoutParams = (LayoutParams) innerWrapper
                    .getLayoutParams();
layoutParams.height = (int) (layoutParams.height * ratio);
innerWrapper.setLayoutParams(layoutParams);
preview.addView(mPreview);

那个innerWrapper 东西是为了显示一个正方形的图片。 setCameraOrientation 将图像调整为与比例对齐。拍照时我使用自动对焦然后...

mCamera.autoFocus(new AutoFocusCallback() {

    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        mCamera.takePicture(null, null, new PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
                                            data.length, options);
                Bitmap finalBitmap;
                float ow = bitmap.getWidth();
                float oh = bitmap.getHeight();
                // At this point, ow and oh are 320 and 240
                float ratio = ow / oh;
                int fw, fh, x, y, angle;
                //It continues, but irrelevant
                ...

这是否与我用来保存预览的 FrameLayout 的布局尺寸有关?因为我定义它是350x350 dp,所以预览应该更大。

最佳答案

傻我。问题是我的 PictureCallback 是作为 jpeg 参数实现的,而不是 raw 参数。在哪里,引用 documentation :

The raw callback occurs when the raw image data is available (NOTE: the data will be null if there is no raw image callback buffer available or the raw image callback buffer is not large enough to hold the raw image). The postview callback occurs when a scaled, fully processed postview image is available (NOTE: not all hardware supports this). The jpeg callback occurs when the compressed image is available.

将整个 PictureCallback 移动到 mCamera.takePicture 的第二个参数而不是第三个参数将是解决问题的途径。但是,您需要 CallbackBuffer 来执行此操作并更改代码以适应缓冲区的使用。所以对于第一个版本,我会坚持使用低质量的图片。

关于android - 相机预览始终返回 320x240 位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15522213/

相关文章:

Android Camera参数setPictureSize导致图片出现条纹

android - 同步 Gradle 时出现 Firebase 错误

objective-c - IOS CGBitmapContextCreate 大位图

c# - 在 .NET 中压缩图像

winapi - HBITMAP 到 BITMAP 的转换

android - 模拟 Android 摄像头

android - 如何在 surfaceview android 中捕获图像后重新加载相机?

android - ffmpeg 构建 : Error during build

android - WebView 停止使用设备字体设置并使用 HTML 中指定的样式

android - 从不同的类运行 runTestOnUiThread