在三星Galaxy S上以肖像模式“拍照并呈现”

时间:2012-09-01 12:09:19

标签: android android-intent galaxy

我想简单地拍一张照片并用我的三星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 个答案:

答案 0 :(得分:2)

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

此问题的一个解决方案是将清单设置为忽略方向更改的更改,您可以使用以下方法执行此操作:

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

如果您使用级别为13的API启动,则可以考虑screenSize获取configChanges清单。

答案 1 :(得分:1)

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

答案 2 :(得分: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();
                    }

答案 3 :(得分: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