将Android拍摄的图像调整为竖屏方向

4
在我的应用程序中,我需要从手机相册页面上传一些图片。我正在使用三星Galaxy Ace,并使用手机的默认相机以纵向模式拍摄了一些图片。在拍摄后,我在应用程序中打开这些图片并尝试在图像视图中显示它们。那些以纵向模式拍摄的图片在图像视图中似乎是横向的。使用"exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION)",我检查了图片方向值为6。使用以下代码,我将以纵向模式在图像视图中显示图片。
 Matrix matrix = new Matrix();
                       matrix.postRotate(90);
                       bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(HomePage._uri));
                       bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                       i.setImageBitmap(bitmap);

但是,在我的应用程序的另一个活动中上传图像并检索它后,似乎仍然处于横向模式。如何在纵向拍摄时上传图像本身?我已经以竖向方式捕获了图像,自己显示了它的竖向,但在上传它时,我需要它本身是竖向的,这样当我检索它时就可以查看它的竖向模式。如何完成此操作(为了捕捉,我没有使用应用程序中的相机,而是在应用程序外部使用手机默认相机进行捕捉)?
1个回答

1
我已经找到了从相册获取并上传图片的解决方案。一些从相册中选择的图片可能看起来是旋转的,在这种情况下,以下解决方案效果很好。
从相册中选择图片。
Intent intent = new Intent(Intent.ACTION_PICK,  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        startActivityForResult(intent, 2); 

在onActivityResult中的下一步

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK )
        {
            f(requestCode == 2)
            {   
                try 
                {
                String [] proj = { MediaStore.Images.Media.DATA };  
                Cursor cursor = managedQuery(data.getData(), proj, null, null, null);  
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
                cursor.moveToFirst();  
                pathInput = cursor.getString(column_index); 

                Appconstants.f = Environment.getExternalStorageDirectory() + "/tmp_siva.jpg";
                ImageUtils.resampleImageAndSaveToNewLocation(pathInput, Appconstants.f);
                }
                catch (Exception ex) 
                {
                Log.e("Exception ex @ try catch",""+ex);
                }
            }
            }             
    }

这是ImageUtils类。
public class ImageUtils 
{
     private ImageUtils() 
     {
     }

     public static void resampleImageAndSaveToNewLocation(String pathInput, String pathOutput) throws Exception 
     {
         Bitmap bmp = resampleImage(pathInput, 800);

         OutputStream out = new FileOutputStream(pathOutput);
         bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    }

    public static Bitmap resampleImage(String path, int maxDim) throws Exception 
    {        
        BitmapFactory.Options bfo = new BitmapFactory.Options(); 
        bfo.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(path, bfo); 

        BitmapFactory.Options optsDownSample = new BitmapFactory.Options();
        optsDownSample.inSampleSize = getClosestResampleSize(bfo.outWidth, bfo.outHeight, maxDim);

        Bitmap bmpt = BitmapFactory.decodeFile(path, optsDownSample);

        Matrix m = new Matrix(); 

        if (bmpt.getWidth() > maxDim || bmpt.getHeight() > maxDim) 
        {           
            BitmapFactory.Options optsScale = getResampling(bmpt.getWidth(), bmpt.getHeight(), maxDim);
            m.postScale((float)optsScale.outWidth  / (float)bmpt.getWidth(), (float)optsScale.outHeight / (float)bmpt.getHeight()); 
            }

        int sdk = new Integer(Build.VERSION.SDK).intValue(); 
        if (sdk > 4) 
        {
            int rotation = ExifUtils.getExifRotation(path);
            if (rotation != 0) 
            { 
                m.postRotate(rotation); 
            }
        }

        return Bitmap.createBitmap(bmpt, 0, 0, bmpt.getWidth(), bmpt.getHeight(), m, true); 
    }

    private static BitmapFactory.Options getResampling(int cx, int cy, int max) 
    {
        float scaleVal = 1.0f;
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        if (cx > cy) 
        {
            scaleVal = (float)max / (float)cx;
        }
        else if (cy > cx) 
        {
            scaleVal = (float)max / (float)cy;
        }
        else 
        {
            scaleVal = (float)max / (float)cx;
        }
        bfo.outWidth  = (int)(cx * scaleVal + 0.5f);
        bfo.outHeight = (int)(cy * scaleVal + 0.5f);
        return bfo;
    }

    private static int getClosestResampleSize(int cx, int cy, int maxDim) 
    {
        /*Log.e("cx",""+cx);
        Log.e("cy",""+cy);*/
        int max = Math.max(cx, cy);

        int resample = 1;
        for (resample = 1; resample < Integer.MAX_VALUE; resample++) 
        {
            if (resample * maxDim > max) 
            {
                resample--;
                break;
            }
        }

        if (resample > 0) 
        {
            return resample;
        }
        return 1;
    }

    public static BitmapFactory.Options getBitmapDims(String path) throws Exception 
    {
        BitmapFactory.Options bfo = new BitmapFactory.Options(); 
        bfo.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(path, bfo); 
        return bfo;
    }
}

这是Exif类

public class ExifUtils 
{
    private ExifUtils() 
    {
    }

    public static int getExifRotation(String imgPath) 
    {
        try 
        {
            ExifInterface exif = new ExifInterface(imgPath);
            String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            if (!TextUtils.isEmpty(rotationAmount)) 
            {
                int rotationParam = Integer.parseInt(rotationAmount);
                switch (rotationParam) 
                {
                    case ExifInterface.ORIENTATION_NORMAL:
                        return 0;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        return 90;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        return 180;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        return 270;
                    default:
                        return 0;
                }
            } 
            else 
            {
                return 0;
            }
        }
        catch (Exception ex) 
        {
            return 0;
        }
    }
}

在图库中选择的图像会被检查其是否为纵向或横向类型,并进行旋转并保存到SD卡的新路径中。为避免OOM问题,它已被调整大小。

但是在从相机捕获并使用图像时,这种方法不起作用,如果有人知道,请在这里发布... - Siva K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接