【问题标题】:"take a picture and present it" in portrait mode on Samsung Galaxy S在三星 Galaxy S 上以人像模式“拍照并呈现”
【发布时间】:2012-08-26 23:50:25
【问题描述】:

我正在尝试简单地拍照并使用我的三星 Galaxy s 将其呈现在 ImageView 中。当我在横向上执行此操作时效果很好,但在纵向上不是。我没有收到任何错误或异常 - 只是没有收到任何东西......关于这个主题有很多问题,它似乎有问题(关于相机方向的问题)但无法找出简单“的最终解决方案”拍照并呈现”代码。 这是我的(有问题的)代码不起作用:

private void setUpListeners() {
    takePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Log.d("onActivityResult", "CAMERA_PIC_REQUEST returned");
            dishImage = (Bitmap) data.getExtras().get("data");
            if (dishImage==null)
                Log.d("onActivityResult", "dishImage==null");
            imageView = (ImageView) findViewById(R.id.dishinfodishimageview);
            imageView.setImageBitmap(dishImage);
            imageView.setVisibility(View.VISIBLE);
            takePicture.setVisibility(View.GONE);
            (new UploadImage()).execute(null);
        }
    } else {
        Log.e("onActivityResult",
                "no able to presenting the picture of the dish");
    }

}

我只需要一个可以工作的代码(在任何设备上)或修复我的代码... 谢谢。

【问题讨论】:

  • 看到这个旧的answer。它可能会帮助你。 :)
  • 要旋转您的位图图像,请参考link
  • 我仍然不明白:在纵向我没有得到旋转的图片...我根本没有得到图片...而不是错误或异常.. .它似乎跳过了行 imageView.setImageBitmap(dishImage);(但在横向它正在工作......)
  • 发现了问题 - 由于某种原因,当恢复活动时在风景中拍摄照片时,未调用 onCreate。 但是,当以纵向拍摄照片时,会调用 onCreate 函数并重置活动变量,因此我没有看到拍摄的照片...有人知道这种行为的原因吗?
  • 有没有人找到解决这种奇怪行为的方法??我也有同样的烦恼,而且...我是堆栈溢出!!

标签: android android-intent galaxy


【解决方案1】:

之所以调用onCreate(),是因为当你在纵向时调用camera Activity,它会改变方向,破坏你之前的Activity。完成onActivityResult()后,您的活动将被重新创建。

这个问题的一个解决方案是设置清单以忽略方向变化,你可以这样做:

<activity android:name=".MyMainActivity"
     android:configChanges="orientation"
     android:label="@string/app_name" />

如果您使用从级别 13 开始的 API,您可以考虑将 screenSize 用于 configChanges 清单。

【讨论】:

    【解决方案2】:

    对于这个问题,我只能建议一个 hack。将您的结果保存在onActivityResult() 的共享首选项中,并在onCreate 期间从共享首选项加载您的内容。我知道这是一个糟糕的解决方案,但这会让你继续前进,直到你找到更好的答案。完成后不要忘记清除您的共享偏好,否则您的活动将始终使用旧数据进行初始化。

    【讨论】:

      【解决方案3】:

      试试下面的代码。它适用于我的三星 Galaxy S2 在 onActivityResult() 中插入下面的代码

      ExifInterface exif = new ExifInterface(cameraimagename);
                          String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
                          int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
                          int rotationAngle = 0;
                          System.out.println("orientation is : "+orientation);
                          System.out.println("ExifInterface.ORIENTATION_ROTATE_90 : "+ExifInterface.ORIENTATION_ROTATE_90);
                          System.out.println("ExifInterface.ORIENTATION_ROTATE_180 : "+ExifInterface.ORIENTATION_ROTATE_180);
                          System.out.println("ExifInterface.ORIENTATION_ROTATE_270 : "+ExifInterface.ORIENTATION_ROTATE_270);
      
                          if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
                          if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
                          if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
                          System.out.println("Rotation Angle is : "+rotationAngle);
                          Matrix matrix = new Matrix();
                         // matrix.setRotate(rotationAngle, (float) photo.getWidth() / 2, (float) photo.getHeight() / 2);
                          matrix.postRotate(rotationAngle);
      
                          Bitmap rotatedBitmap=null;
                          try {
                              rotatedBitmap = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                          } catch (Exception e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
      

      【讨论】:

      • cameraimagename = 图片路径(String),Photo是之前生成的位图对象
      • 我尝试了类似的代码,对我来说速度很慢,你是否遇到同样的问题?
      • 不,@Helin Wang 在我的 Galaxy S2 设备上完美运行
      【解决方案4】:

      很容易检测图像方向并使用以下方法替换位图:

       /**
       * Rotate an image if required.
       * @param img
       * @param selectedImage
       * @return 
       */
      private static Bitmap rotateImageIfRequired(Context context,Bitmap img, Uri selectedImage) {
      
          // Detect rotation
          int rotation=getRotation(context, selectedImage);
          if(rotation!=0){
              Matrix matrix = new Matrix();
              matrix.postRotate(rotation);
              Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
              img.recycle();
              return rotatedImg;        
          }else{
              return img;
          }
      }
      
      /**
       * Get the rotation of the last image added.
       * @param context
       * @param selectedImage
       * @return
       */
      private static int getRotation(Context context,Uri selectedImage) {
          int rotation =0;
          ContentResolver content = context.getContentResolver();
      
      
          Cursor mediaCursor = content.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                  new String[] { "orientation", "date_added" },null, null,"date_added desc");
      
          if (mediaCursor != null && mediaCursor.getCount() !=0 ) {
              while(mediaCursor.moveToNext()){
                  rotation = mediaCursor.getInt(0);
                  break;
              }
          }
          mediaCursor.close();
          return rotation;
      }
      

      为避免因大图像而无法记忆,我建议您使用以下方法重新缩放图像:

      private static final int MAX_HEIGHT = 1024;
      private static final int MAX_WIDTH = 1024;
      public static Bitmap decodeSampledBitmap(Context context, Uri selectedImage)
              throws IOException {
      
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
          BitmapFactory.decodeStream(imageStream, null, options);
          imageStream.close();
      
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT);
      
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          imageStream = context.getContentResolver().openInputStream(selectedImage);
          Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
      
          img= rotateImageIfRequired(img, selectedImage);
          return img;
      } 
      

      由于 Android 操作系统问题,无法使用 ExifInterface 获取方向: https://code.google.com/p/android/issues/detail?id=19268

      【讨论】:

        猜你喜欢
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 2012-01-24
        • 1970-01-01
        • 2011-05-13
        相关资源
        最近更新 更多