图像无法在ImageView中正确显示

时间:2015-10-20 09:39:06

标签: android camera imageview orientation

我的android项目有点问题,希望能找到你们的解决方案。

我从手机图库中获取图像,或从前面或稀有相机拍摄照片并将其设置在ImageView中。一切都很好,我可以从画廊中获取图像,并可以从相机拍摄图片并在图像视图中设置它。

问题:

我面临的问题是图像的方向不是我想要的。当我从手机的前置摄像头拍摄照片时,图像从右到左显示,而不是从上到下。请看下图:

Image Taken from the Front camera and it is showing like this in the Image View

当我在Portrait中捕捉图像时(从稀有相机中),它在ImageView中从左到右显示,而不是从上到下。

Image Taken in Portrait mode from the rare camera and it is showing like this

当我在横向模式下捕捉图像时,它在ImageView i-e从上到下显示正常。

landscape 这是横向模式图像输出..

我不知道如何修复它...所以我缺少什么或者我该怎么做才能在ImageView的Top to Bottom视图中制作所有图像。

以下是我的XML和Java文件:

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imgProfilePic"
    android:layout_above="@+id/btnGetPic"
    />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnGetPic"
    android:layout_alignParentBottom="true"
    android:text="Get pictures from Gallery or Camera"/>

</RelativeLayout>

Java文件:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

Button btnGetPic;
ImageView imgProfilePic;
private static int REQUEST_GALLERY = 1;
private static int REQUEST_CAMERA = 2;
RoundTheImage roundedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnGetPic = (Button) findViewById(R.id.btnGetPic);
    imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);

    btnGetPic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            Bundle bundle = new Bundle();
            bundle = data.getExtras();
            Bitmap bmp;
            bmp = (Bitmap) bundle.get("data");
            Bitmap resized = Bitmap.createScaledBitmap(bmp, 1000, 1000, true);
            roundedImage = new RoundTheImage(resized);
            imgProfilePic.setImageDrawable(roundedImage);
        } else if (requestCode == REQUEST_GALLERY && null != data) {


            try {
                InputStream inputStream = getContentResolver().openInputStream(data.getData());
                Bitmap bmp;
                bmp = BitmapFactory.decodeStream(inputStream);

                Bitmap resized = Bitmap.createScaledBitmap(bmp, 1000, 1000, true);
                roundedImage = new RoundTheImage(resized);
                imgProfilePic.setImageDrawable(roundedImage);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

private void selectImage() {
    final CharSequence[] items = {"Take Photo from Camera", "Choose from Library", "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo from Camera")) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent,REQUEST_CAMERA); // 1 as REQUEST_CAMERA
                }
            } else if (items[item].equals("Choose from Library"))  {

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), REQUEST_GALLERY);


            } else if (items[item].equals("Cancel"))                {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

}

我使用这个类来围绕图像的边缘(我从一个SO答案得到这个)

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

public class RoundTheImage extends Drawable {
private final Bitmap mBitmap;
private final Paint mPaint;
private final RectF mRectF;
private final int mBitmapWidth;
private final int mBitmapHeight;

public RoundTheImage(Bitmap bitmap) {
    mBitmap = bitmap;
    mRectF = new RectF();
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mPaint.setShader(shader);

    mBitmapWidth = mBitmap.getWidth();
    mBitmapHeight = mBitmap.getHeight();
}

@Override
public void draw(Canvas canvas) {
    canvas.drawOval(mRectF, mPaint);
}

@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    mRectF.set(bounds);
}

@Override
public void setAlpha(int alpha) {
    if (mPaint.getAlpha() != alpha) {
        mPaint.setAlpha(alpha);
        invalidateSelf();
    }
}

@Override
public void setColorFilter(ColorFilter cf) {
    mPaint.setColorFilter(cf);
}

@Override
public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
}

@Override
public int getIntrinsicWidth() {
    return mBitmapWidth;
}

@Override
public int getIntrinsicHeight() {
    return mBitmapHeight;
}

public void setAntiAlias(boolean aa) {
    mPaint.setAntiAlias(aa);
    invalidateSelf();
}

@Override
public void setFilterBitmap(boolean filter) {
    mPaint.setFilterBitmap(filter);
    invalidateSelf();
}

@Override
public void setDither(boolean dither) {
    mPaint.setDither(dither);
    invalidateSelf();
}

public Bitmap getBitmap() {
    return mBitmap;
}

}

2 个答案:

答案 0 :(得分:1)

How to set Android camera orientation properly?

看一下,它应该回答你的问题。基本上你需要根据手机传感器告诉应用程序相机的方向,并且应该允许你以正确的方向捕捉。希望它有所帮助!

答案 1 :(得分:1)

感谢 hipkiss 让我指向正确的方向。

请注意:这不是最好的解决方案,可能无法很好地优化,也可能不适用于所有设备。

对于像我这样的新手,这是我的最终代码。所以有一天它可能对某人有帮助:)。

首先在按钮的点击事件或警告对话框等中执行此操作

 Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 startActivityForResult(i, REQUEST_GALLERY);

声明 REQUEST_GALLERY

 private static int REQUEST_GALLERY = 1;

现在在你的 onActivityResult做这样的事情:

try {

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgPath = cursor.getString(columnIndex);
                cursor.close();

                int rotation = getCameraPhotoOrientation(MainActivity.this,selectedImage,imgPath);

                Matrix matrix = new Matrix();
                matrix.postRotate(rotation);

                Bitmap original = BitmapFactory.decodeFile(imgPath);
                Bitmap myFinalImg = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);

                ImageView imgView = (ImageView) findViewById(R.id.imgProfilePic);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(myFinalImg);

 } catch (Exception e) {
                Toast.makeText(this, "Unable to load the image", Toast.LENGTH_LONG).show();
                 }

此函数将返回旋转(即应该旋转多少)

 public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath) {

    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());

        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                rotate = 0;
                break;
            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 (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

@Pros那里请修复/改进这个答案,这样可以帮助其他人,让我知道如何改进它:)

修改

不要忘记在AndroidManifest文件中添加权限:

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>