简体   繁体   English

如何使用三星在三星手机中设置拍摄的图像为人像模式

[英]how to set captured image using android in sumsung device is portrait mode

I'm trying below code to set captured image using android in sumsung device is portrait mode, but its set to landscape mode. 我正在尝试下面的代码以在sumsung设备中使用android将捕获的图像设置为纵向模式,但将其设置为横向模式。 Below code works fine for other devices excepts for samsung and sony. 下面的代码对三星和索尼以外的其他设备都适用。

Matrix mat = new Matrix();
ExifInterface exif = new ExifInterface(yourimagepath);
String orientstring = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientstring != null ? Integer.parseInt(orientstring) : ExifInterface.ORIENTATION_NORMAL;
int rotateangle = 0;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90) 
            rotateangle = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_180) 
            rotateangle = 180;
if(orientation == ExifInterface.ORIENTATION_ROTATE_270) 
            rotateangle = 270;

mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);

File f = new File(yourimagepath);       
Bitmap bmpPic = BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
Bitmap bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true); 

Is there is any other solution for same? 还有其他解决方案吗? Please suggest. 请提出建议。

use this in your manifest file in activity tag.. 在活动标签的清单文件中使用它。

android:screenOrientation="portrait" android:screenOrientation =“ portrait”

It can set portrait mode for all devices. 它可以为所有设备设置人像模式。

Use ExifInterface to check the orientation of the image as stored in the device. 使用ExifInterface检查存储在设备中的图像方向。

int rotate = 0;
try {
    File imageFile = new File(uploadFile.getPath());
    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();
}

Then using matrix rotate the bitmap to the actual portrait or landscape as stored in device. 然后使用矩阵将位图旋转到存储在设备中的实际肖像或风景。

Matrix matrix = new Matrix();

matrix.postRotate(rotate);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;

Bitmap bmp = BitmapFactory.decodeFile(uploadFile.getPath(), options);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

rotatedBitmap is the bitmap with correct orientation. 经旋转的位图是具有正确方向的位图。

Hope it helps. 希望能帮助到你。

Try this code. 试试这个代码。 I was used it on different phones include samsung and it works perfectly: 我曾在包括三星在内的其他手机上使用过它,效果非常好:

public Bitmap rotateImage(Bitmap bitmap) throws IOException {
        //Rotate the image to get in correct position
        ExifInterface exif = new ExifInterface(mImagePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
        } else if (orientation == 8) {
            matrix.postRotate(270);
        } else if (orientation == 3) {
            matrix.postRotate(180);
        }

        Bitmap rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return rotatedImage;
    }

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查是否使用Android相机以纵向或横向拍摄图像? - How to check whether an image is captured in portrait mode or landscape mode using camera in android? 如果应用程序的默认方向设置为纵向模式,我们如何检测使用 CameraX 捕获的图像的方向 - How do we detect the Orientation of Image captured using CameraX if Application's default orientation is set to Portrait Mode 如何在纵向模式下设置拍摄的图像 - How to set captured images in portrait mode android捕获的图像是在肖像中 - android captured image to be in portrait 以人像模式拍摄的Android视频无法以人像模式播放 - Android video captured in portrait mode is not playing in portrait mode 相机预览处于纵向模式,但旋转的图像被旋转 - Camera preview is in portrait mode but image captured is rotated 从相机拍摄的图像应为人像模式 - Captured image from camera should be in portrait mode 如何使用 php 将使用 android 设备捕获的图像上传到 mysql 数据库? - How to upload a captured image with android device to mysql database using php? 从Android中的“图库”中选择后,如何强制将肖像设置为肖像模式? - How to Set image in portrait mode compulsory after selecting from Gallery in Android? 如何缩放图像以在Android平板电脑和纵向模式下显示为背景图像 - How to scale an image to show as a background image on android tablet and portrait mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM