43
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ギャラリーから取得した写真を正しい向きで表示

Last updated at Posted at 2014-02-28

アルバムにある写真に加工するプログラムを作るために、
ギャラリーから画像を取得する
を試したら、簡単に取得できた!
が、なぜか全て横向き表示・・・。
調べてみると、写真の向きが機種によって違い、向きを指定する必要があるらしい。

で、
ギャラリーから呼び出す画像の向きを取得する
を参考に向きを取得。
ExifInterfaceを使ったサンプルを記載しているサイトはいろいろあったが、画像ファイルの場所がわかっている前提のばかりで、ギャラリーから取得した画像のファイル名を取得しているのはここくらい。

向きがわかったので、
Exif情報を利用して、写真の向きを正しく表示する
を参考に、縮尺なども加えてできたのがこれ。

package jp.murapon.sample;

import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
    private static final int REQUEST_GALLERY = 0;
    private ImageView imgView;
    private float viewWidth;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imgView = (ImageView)findViewById(R.id.imgview_id);
        // ギャラリー呼び出し
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, REQUEST_GALLERY);

        // ウィンドウマネージャのインスタンス取得
        WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
        // ディスプレイのインスタンス生成
        Display disp = wm.getDefaultDisplay();
        viewWidth = disp.getWidth();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
            try {
                InputStream in = getContentResolver().openInputStream(data.getData());
                Bitmap bitmap = BitmapFactory.decodeStream(in);
                in.close();

                //dataからfilepathへの変換  
                String[] columns = {MediaStore.Images.Media.DATA };  
                Cursor c = getContentResolver().query(data.getData(), columns, null, null, null);  
                c.moveToFirst();  
                ExifInterface exifInterface = new ExifInterface(c.getString(0));
                // 向きを取得
                int orientation = Integer.parseInt(exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION));

                imgView.setScaleType(ScaleType.MATRIX);
                imgView.setImageBitmap(bitmap);

                // 画像の幅、高さを取得
                int wOrg = bitmap.getWidth();
                int hOrg = bitmap.getHeight();
                imgView.getLayoutParams();
                LayoutParams lp = (LayoutParams) imgView.getLayoutParams();

                float factor;
                Matrix mat = new Matrix();
                mat.reset();
                switch(orientation){
                    case 1://only scaling
                        factor = (float)viewWidth/(float)wOrg;
                        mat.preScale(factor, factor);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 2://flip vertical
                        factor = (float)viewWidth/(float)wOrg;
                        mat.postScale(factor, -factor);
                        mat.postTranslate(0, hOrg*factor);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 3://rotate 180
                        mat.postRotate(180, wOrg/2f, hOrg/2f);
                        factor = (float)viewWidth/(float)wOrg;
                        mat.postScale(factor, factor);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 4://flip horizontal
                        factor = (float)viewWidth/(float)wOrg;
                        mat.postScale(-factor, factor);
                        mat.postTranslate(wOrg*factor, 0);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 5://flip vertical rotate270
                        mat.postRotate(270, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, -factor);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                    case 6://rotate 90
                        mat.postRotate(90, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, factor);
                        mat.postTranslate(hOrg*factor, 0);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                    case 7://flip vertical, rotate 90 
                        mat.postRotate(90, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, -factor);
                        mat.postTranslate(hOrg*factor, wOrg*factor);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                    case 8://rotate 270
                        mat.postRotate(270, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, factor);
                        mat.postTranslate(0, wOrg*factor);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                }
                imgView.setLayoutParams(lp);
                imgView.setImageMatrix(mat);
                imgView.invalidate();
            } catch (Exception e) {
                Log.e(e.toString(), "Error");
            }
        } 
    }
}
43
42
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?