android - S3(GT-I9300) 相机预览支架错误

标签 android camera

我们基于 android.hardware.Camera 类创建了一个自定义相机。 当我们按下“拍照”按钮时,预期的行为是拍照并在查看屏幕上看到静止拍摄的照片。但是,当我们到达查看屏幕时,相机仍然可以工作/取景器显示“实时”镜头(而不是静止的查看)。 支架似乎不起作用。

这已经在一些 Samsung Galaxy S3 手机 (GT-I9300) 上观察到;但奇怪的是,在所有其他型号上一切正常(静止图像按应有的方式出现在查看屏幕中)。

最佳答案

到目前为止,我的解决方案是创建一个布局,其中 View 占据与表面 View 相同的 View 。

然后我得到预览的 View 边界(在我的例子中它是屏幕的大小)

//Get our screen size
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    Point size = new Point();
    try {
        display.getSize(size);
        w_width = size.x;
        w_height = size.y;
    } catch (NoSuchMethodError e) {
        w_width = display.getWidth();
        w_height = display.getHeight();
    }

然后我将回调设置为:

callback = new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] bytes, Camera camera) {
                //Get the view bounds to determine how to manipulate this image
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

                //If the image is smaller than the screen, don't make the image bigger
                int finalWidth = (w_width < options.outWidth) ? w_width : options.outWidth;
                int finalHeight = (w_height < options.outHeight) ? w_height : options.outHeight;
                int inSampleSize = 1;
                options = new BitmapFactory.Options();
                if (finalHeight > w_height || finalWidth > w_width) {

                    // Calculate ratios of height and width to requested height and width
                    final int heightRatio = Math.round((float) finalHeight / (float) w_height);
                    final int widthRatio = Math.round((float) finalWidth / (float) w_width);

                    // Choose the smallest ratio as inSampleSize value, this will guarantee
                    // a final image with both dimensions larger than or equal to the
                    // requested height and width.
                    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
                }

                options.inSampleSize = inSampleSize;

                //Decode the smaller image
                Bitmap b = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

                //Rotate the image correctly
                Matrix mat = new Matrix();
                mat.postRotate(orientation);
                Bitmap newBitmap = Bitmap.createBitmap(b, 0, 0, options.outWidth, options.outHeight, mat, true);
                imagePreview.setBackgroundDrawable(new BitmapDrawable(newBitmap));
                imagePreview.setVisibility(View.VISIBLE);
            }
        };

所以基本上 surface view 永远不会停止显示预览,但是 imageview 会隐藏它所以用户看不到它。

然后,如果用户决定不保留图像,那么我只需重新隐藏 View ,surfaceView 将继续显示实时预览。幸运的是,GS3 似乎并不介意有人调用“camera.startPreview();”即使预览已经开始。

我真的不喜欢这个答案,但这是我目前在 galaxy S3 上工作的全部内容。它适用于我尝试过的旧设备(2.3 及更高版本),但它绝对只是一种解决方法。

关于android - S3(GT-I9300) 相机预览支架错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14821950/

相关文章:

java - org.json.JSONException : Value <div of type java. 调试时lang.String无法转换为JSONObject

java - 无法解析类 com.android.builder.core.DefaultManifestParser

android - 仅当上一个通知已被取消时才显示通知

java - 如何执行具有异步任务返回值的方法?

Android Camera - 应用程序传递了 NULL 表面

android - fragment 在 PictureTake 上分离

android - 如何捕获所需分辨率的视频?

java - 如何使用 Java 文件中的 R.Color 在 Android Studio 中更改操作栏 setBackgrounddrawable 颜色?

android - 移动手机时的图像稳定 - Android

java - 如何使用额外选项(如全景)实现相机功能?