当前位置: 首页 > 知识库问答 >
问题:

在Android中设置相机拍摄的图像?

方祺
2023-03-14

我正在开发一个应用程序,在这个应用程序中,我以纵向方向拍摄照片,问题是当我稍后检索图像时,它是横向方向的(图片已逆时针旋转90度)。我曾经在课下使用过,但这里每次都是0(零)。所以,我不知道怎么解决它。

public Bitmap rotateBitmapOrientation(String photoFilePath) {

// Create and configure BitmapFactory
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
// Read EXIF Data
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
// Rotate Bitmap
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
// Return result
return rotatedBitmap;
}

共有2个答案

尹冠宇
2023-03-14

使用下面的代码来解决您的问题-

int rotate;
String URI = getImageURI();
                    try {
                        File file = new File(URI);
                        ExifInterface exif = new ExifInterface(
                                file.getAbsolutePath());
                        int orientation = exif.getAttributeInt(
                                ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL);
                        Log.v("my", "Exif orientation: " + orientation);
                        switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            Log.v("", "rotated " +270);
                            rotate = 270;
                            Log.e("rotate", ""+rotate);
                            ImageOrientation(file,rotate);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            Log.v("", "rotated " +180);
                            rotate = 180;
                            Log.e("rotate", ""+rotate);
                            ImageOrientation(file,rotate);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            Log.v("", "rotated " +90);
                            rotate = 90;
                            ImageOrientation(file,rotate);
                            break;

                        case 1:
                            Log.v("", "rotated1-" +90);
                            rotate = 90;
                            ImageOrientation(file,rotate);
                            break;

                        case 2:
                            Log.v("", "rotated1-" +0);
                            rotate = 0; 
                            ImageOrientation(file,rotate);
                            break;
                        case 4:
                            Log.v("", "rotated1-" +180);
                            rotate = 180;
                            ImageOrientation(file,rotate);
                            break;

                        case 0:
                            Log.v("", "rotated 0-" +90);
                            rotate = 90;
                            ImageOrientation(file,rotate);
                            break;
                        }
                        filePath = file.getAbsolutePath();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
   private void ImageOrientation(File file,int rotate){
    try {
        FileInputStream fis = new FileInputStream(file);
        filePath = file.getAbsolutePath();
        Bitmap photo = BitmapFactory.decodeStream(fis);
        Matrix matrix = new Matrix();
        matrix.preRotate(rotate); // clockwise by 90 degrees
        photo = Bitmap.createBitmap(photo , 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
        customerImageView.setImageBitmap(photo);
        imageBitmap = photo;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

希望它能帮助你。

谢志文
2023-03-14

在stackoverflow上有几种可用的方法,但我使用的是它们的混合,如果您希望图像处于捕获的方向,可以使用以下指令和类来执行此操作

你的onActivityResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case SELECT_IMAGE_FROM_CAMERA:
            if (requestCode == SELECT_IMAGE_FROM_CAMERA
                    && resultCode == RESULT_OK) {
                int targetW = reviewImageView.getWidth();
                int targetH = reviewImageView.getHeight();
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                int photoW = bmOptions.outWidth;
                int photoH = bmOptions.outHeight;

                // Determine how much to scale down the image
                int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

                // Decode the image file into a Bitmap sized to fill the View
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inSampleSize = scaleFactor;
                bmOptions.inPurgeable = true;

                Bitmap rotatedBitmap = decodeFile(new File(mCurrentPhotoPath),
                        photoW, photoH, getImageOrientation(mCurrentPhotoPath));
                reviewImageView.setImageBitmap(rotatedBitmap);
                uploadMessage.setVisibility(View.INVISIBLE);
                UploadSuccess.setVisibility(View.INVISIBLE);
            } else if (requestCode == SELECT_IMAGE_FROM_CAMERA
                    && resultCode == RESULT_CANCELED) {
                mCurrentPhotoPath = null;
                photo = null;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

帮助方法

public static int getImageOrientation(String imagePath) {
        int rotate = 0;
        try {

            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rotate;
    }

public static Bitmap decodeFile(File f, double REQUIRED_WIDTH,
            double REQUIRED_HEIGHT, int rotation) {
        try {
            if (REQUIRED_WIDTH == 0 || REQUIRED_HEIGHT == 0) {
                return BitmapFactory.decodeFile(f.getAbsolutePath());
            } else {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), o);

                o.inSampleSize = calculateInSampleSize(o, REQUIRED_WIDTH,
                        REQUIRED_HEIGHT);

                o.inJustDecodeBounds = false;
                o.inPurgeable = true;
                Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath(), o);
                if (rotation != 0)
                    b = rotate(b, rotation);
                if (b.getWidth() > REQUIRED_WIDTH
                        || b.getHeight() > REQUIRED_HEIGHT) {
                    double ratio = Math.max((double) b.getWidth(),
                            (double) b.getHeight())
                            / (double) Math
                                    .min(REQUIRED_WIDTH, REQUIRED_HEIGHT);

                    return Bitmap.createScaledBitmap(b,
                            (int) (b.getWidth() / ratio),
                            (int) (b.getHeight() / ratio), true);
                } else
                    return b;
            }

        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        return null;
    }

public static int calculateInSampleSize(BitmapFactory.Options options,
            double reqWidth, double reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((height / inSampleSize) > reqHeight
                    || (width / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        inSampleSize = Math.max(1, inSampleSize / 2);
        return inSampleSize;
    }

    public static Bitmap rotate(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees, (float) b.getWidth() / 2,
                    (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                // We have no memory to rotate. Return the original bitmap.
            }
        }
        return b;
    }

您可以根据需要定制每个方法,我的代码遵循android指南,所以相当长

 类似资料:
  • 所以我遇到了一个问题,前面在我提问的问题中提到过:将图像(ACTION_image_CAPTURE)上载到Firebase存储 我对这个问题进行了更多的搜索,并应用了Android Studio文档:https://developer.android.com/training/camera/photobasics.html#TaskPhotoView 所以,在您阅读代码之前,我基本上想说一下需要什

  • 我一直在开发一个摄像头应用程序,所有的工作都是拍摄和保存照片。但我希望在拍摄照片时显示的Imageview能够保存在实际拍摄的照片中。可能吗? 现在,我一直试图让图像视图与我绘制相机预览的布局相同。 我想,也许如果它画在与相机相同的视图上,那么它也会看到图像和照片。 有没有人知道这是否可能,或者我必须采取另一种方法?如果是这样,链接到其他解决方案如何做到这一点将不胜感激:) 谢谢 编辑:我一直遵循

  • 在我的应用程序中,用户从相机捕获图像或从图库中选择图像,然后将其转换为pdf并上载到服务器,现在我的问题是,从相机捕获的图像在某些设备上旋转,我有代码尝试解决此问题,但它不起作用 任何建议或问题在哪里,没有错误代码或任何东西

  • 问题内容: 我环顾四周,但似乎并没有解决这个非常恼人的问题的可靠答案。 我以纵向拍摄照片,然后单击“保存/放弃”,按钮也以正确的方向放置。问题是当我随后在横向上检索图像时(图像已逆时针旋转90度) 我不想强迫用户以特定方向使用相机。 有没有办法检测照片是否以人像模式拍摄,然后解码位图并将其向上翻转? 问题答案: 照片始终以相机内置在设备中的方向拍摄。为了使图像正确旋转,您必须读取存储在图片中的方向

  • 我必须做一个按钮,将提供从画廊或从相机选择图像。 结果是有效的。如果我从图库中选择,图像查看器将查看它,如果我选择从相机拍照,它也有效。问题是,在我的show FileChooser()方法中,我所有的意图都在同时运行,所以当我从图库中选择时,相机仍然运行。我选择相机,图库也在打开。我认为我应该在切换案例模式下实现我的代码,但我不明白如何做到这一点。请帮助解决我的初学者问题。

  • 问题内容: 我已经在此问题上停留了一段时间,并查看了各种教程以寻求帮助,但尚未成功。 我实质上已经在我的 应用程序中 利用了该功能来 拍照 并显示它, 但是 它无法保存拍照。 这是包含我试图使其根据教程发挥作用的代码: 我已经在文件中包含了所有必要的内容。 问题答案: File imagesFolder = new File(Environment.getExternalStorageDirect