“take a picture and present it” in portrait mode o

2019-07-31 07:30发布

我想简单地拍照,并与我的三星Galaxy S的ImageView的介绍吧。 当我这样做,对景观,但不是肖像它的正常工作。 我没有得到任何错误或异常 - 只是没有得到任何东西......有很多关于这个主题的问题,它似乎是有问题的(一些关于相机方向),但counldn't想出了一个简单的最终解决方案“拍照并将其显示”的代码。 这里是我的(问题)的代码不工作:

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");
    }

}

我只是需要一个工作(任何设备)代码或修正我的代码... THX。

Answer 1:

其原因onCreate()被调用是因为当你调用纵向方向在相机的活动,它会改变方向,破坏先前的活动。 完成后onActivityResult()您的活动将被重新创建。

一个解决这个问题的是设置明显的忽略与方向的变化而改变,你可以使用这个做到这一点:

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

如果您使用的API与13级开始,你可以考虑screenSize为明显的configChanges。



Answer 2:

我只能建议这个问题的破解。 保存结果的共享偏好在onActivityResult()和期间onCreate加载从共享偏好您的内容。 我知道这是一个坏的解决方案,但是这将让你去,直到你找到一个更好的答案。 而且不要忘了清除您的共享偏好一旦你完成,否则你的活动将始终与旧数据初始化。



Answer 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();
                    }


Answer 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;
} 

这不是更多钞票用ExifInterface获得方位,因为Android操作系统问题: https://code.google.com/p/android/issues/detail?id=19268



文章来源: “take a picture and present it” in portrait mode on Samsung Galaxy S