2015-10-20 76 views
2

我的Android项目有点问题我希望我会从你们那里找到一个解决办法。图像没有正确显示在ImageView中

我从手机库中获取图像,或者从前端或罕见相机拍摄照片并将其设置在ImageView中。一切工作正常,我可以从画廊获取图像,并可以从相机拍摄照片并将其设置在图像视图中。

问题:

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

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

当我捕捉人像图像(罕见相机)则显示为左到右的ImageView的不是自上而下。

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

当我捕获在风景模式图像中,示出在ImageView的I-E从上到下正常。

landscape 这是风景模式图像输出..

我不知道如何解决它..我错过了那么什么,或者我应该怎么做才能让所有的图像在顶部的ImageView到底部视图。

这里是我的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; 
} 

} 

回答

1

How to set Android camera orientation properly?

看看那个,它应该回答你的问题。实质上,您需要根据手机传感器告诉应用程序相机的方向,并且应该允许您以正确的方向进行捕捉。希望能帮助到你!

+1

谢谢你指点我朝着正确的方向。我花了几个小时,现在我想出了如何设置方向。 – Hashoo

1

感谢hipkiss指向我朝着正确的方向。

请注意:这不是最好的解决方案,它可能没有很好的优化,它可能不适用于每个设备。

对于像我这样的新手来说,我的最终代码看起来像这样。因此,它可以帮助别人有一天:)

首先做到这一点在你的按钮的Click事件或警告对话框等

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

onActivityResult声明 REQUEST_GALLERY作为

private static int REQUEST_GALLERY = 1; 

现在做这样的事情:

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"/> 
+0

OMG !!!!!!!!!我简直不敢相信你是个天才!我正在寻找解决方案2天,没有任何问题,但现在,随着您的回答,这对我来说是完美的!谢谢!!! <3 <3 S2 S2 <3 –