【问题标题】:saving rotated Bitmap android保存旋转的位图android
【发布时间】:2011-02-28 22:21:13
【问题描述】:

(如果我完全错了,请原谅我,我是新手) 我正在展示一些使用MediaStore.ACTION_IMAGE_CAPTURE 拍摄的照片。我尝试在拍照时禁用自动定向,但它似乎不起作用,我使用了

putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)

这迫使我旋转一些拍摄的照片。然后我将这些照片保存到 SDCARD。我的问题是我不想每次用户加载照片时都旋转它们。我尝试使用此代码创建一个新的位图,该位图将保存在“旋转”状态。它在模拟器上运行,但在我的 HTC 上崩溃。我认为这是一个内存问题。有没有办法有效地做到这一点?更好的是,有没有什么方法可以在使用 Camera Intent 拍照时真正禁用自动定向?

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg");
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
     Matrix mtx = new Matrix();
     mtx.postRotate(90);
     Bitmap rotatedBitmap = Bitmap.createBitmap(Bitmap.createBitmap(tempBitmap, 0, 0, 
                                                         tempW, tempH, mtx, true));

} else{
  //...
}

【问题讨论】:

    标签: android bitmap camera android-intent rotation


    【解决方案1】:

    和上面一样,但他忘记了最后一行的一些代码

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4; //1/4 of the original image
    
    tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg", options);
    int tempW = tempBitmap.getWidth();
    int tempH = tempBitmap.getHeight();
    
    if (tempW>tempH) {
    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    Bitmap rotatedBitmap = **Bitmap.createBitmap**(tempBitmap, 0,0,tempW, tempH, mtx, true);
    

    【讨论】:

      【解决方案2】:

      尝试缩小您正在使用的图像可能是内存问题。有关可能的解决方案,请参见下文。

      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inSampleSize = 4; //1/4 of the original image
      
      tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg", options);
      int tempW = tempBitmap.getWidth();
      int tempH = tempBitmap.getHeight();
      
      if (tempW>tempH) {
          Matrix mtx = new Matrix();
          mtx.postRotate(90);
          Bitmap rotatedBitmap = (tempBitmap, 0,0,tempW, tempH, mtx, true);
      

      【讨论】:

      • 最好指出您为什么认为缩小图像是一种选择。
      【解决方案3】:

      通过以下代码,

      要停止对图像的评价,请使用以下代码 -

      private int getImageOrientation() 
      {
          final String[] imageColumns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
          final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
          Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                  imageColumns, null, null, imageOrderBy);
      
          if (cursor.moveToFirst()) {
              int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
              System.out.println("orientation===" + orientation);
              cursor.close();
              return orientation;
          } else {
              return 0;
          }
      }
      

      如果您遇到任何问题,请参考以下链接

      http://androidlift.info/2016/01/07/android-camera-image-capturing-and-uploading-to-php-server/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        相关资源
        最近更新 更多