8

我试图简单地拍照并用我的三星 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");
    }

}

我只需要一个有效的代码(在任何设备上)或修复我的代码......谢谢。

4

4 回答 4

2

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

解决此问题的一种方法是将清单设置为忽略方向更改时的更改,您可以使用以下方法来做到这一点:

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

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

于 2012-11-21T22:29:47.937 回答
1

试试下面的代码..它适用于三星 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();
                    }
于 2013-04-09T06:45:26.763 回答
1

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

于 2012-09-08T20:29:47.687 回答
0

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

 /**
 * 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

于 2014-02-13T11:37:09.293 回答