简体   繁体   English

三星设备问题中的相机方向

[英]Camera orientation in samsung device issue

I am successfully clicking the image and saving it in the card. 我成功点击图片并将其保存在卡片中。 It is working perfectly in all the devices, but issue is that when I am testing it in Samsung device, then as I am clicking image in the portrait mode then it is saving image by default in the landscape mode. 它在所有设备上都运行良好,但问题是当我在三星设备上测试时,然后当我在纵向模式下点击图像时,它默认在横向模式下保存图像。 But I am not doing any type of rotating code. 但我没有做任何类型的旋转代码。 So please help me. 所以请帮助我。 How can I solve it?? 我该怎么解决?

private void takePicture() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    try {
        Uri mImageCaptureUri = null;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mImageCaptureUri = Uri.fromFile(mFileTemp);
        } else {
            mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
        }
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    } catch (ActivityNotFoundException e) {

        Log.d("", "cannot take picture", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {

        return;
    }

    Bitmap bitmap;

    switch (requestCode) {

    case REQUEST_CODE_TAKE_PICTURE:

        startCropImage();
        break;
    case REQUEST_CODE_CROP_IMAGE:

        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        if (path == null) {

            return;
        }

        bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());
        resizedBitmap = ScalingUtilities.createScaledBitmap(bitmap, 320,
                320, ScalingLogic.CROP);
        imgPreview.setImageBitmap(resizedBitmap);
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


private void startCropImage() {

    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra(CropImage.ASPECT_X, 2);
    intent.putExtra(CropImage.ASPECT_Y, 2);

    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}

I added following permissions in the manifest. 我在清单中添加了以下权限。

   <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I solved my bug, actually I use one trick. 我解决了我的错误,实际上我使用了一个技巧。 I took the manufactured name programtically, like this 我像这样以程序化的方式取得了制造的名称

 String device_name = Build.MANUFACTURER;

After that I saved it in the sharedpreferences object. 之后我将它保存在sharedpreferences对象中。

 deviceIdPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
    SharedPreferences.Editor   editor = deviceIdPreferences.edit();
    editor.putString("Device_id",deviceId);
    editor.putString("device_name",device_name);
    editor.commit();

After that in the destination java file, I extrat the value from sharedpreference. 之后在目标java文件中,我从sharedpreference中提取值。

preferences = this.getSharedPreferences(
            "USER_INFO", Context.MODE_PRIVATE);
    device_name = preferences.getString("device_name", "Empty");

and then 然后

    mBitmap = getBitmap(mImagePath);
    if(device_name.equals("samsung")){

            switch (Integer.parseInt(gotOrientation)) {

                case ExifInterface.ORIENTATION_ROTATE_90:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
                default:
                    mBitmap = Util.rotateImage(mBitmap, 90);
                    break;
            }

        }




 private Bitmap getBitmap(String path) {

    Uri uri = getImageUri(path);
    InputStream in = null;
    try {
        in = mContentResolver.openInputStream(uri);

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(in, null, o);
        in.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        in = mContentResolver.openInputStream(uri);
        Bitmap b = BitmapFactory.decodeStream(in, null, o2);
        in.close();

        return b;
    } catch (FileNotFoundException e) {
        Log.e(TAG, "file " + path + " not found");
    } catch (IOException e) {
        Log.e(TAG, "file " + path + " not found");
    }
    return null;
}

Many devices -- but not all -- will not save portrait images. 许多设备 - 但不是全部 - 不会保存肖像图像。 They will save landscape images with an EXIF header telling the image viewer to rotate the image to portrait. 他们将使用EXIF标题保存风景图像,告诉图像查看器将图像旋转为肖像。 Not all image viewers handle this. 并非所有图像查看器都能处理此问题

This is part of the reason I wrote the CWAC-Camera library , to provide uniform camera output from different devices. 这是我编写CWAC-Camera库的原因之一,它提供来自不同设备的统一摄像头输出。

The code that I use for this is reminiscent of what Haresh points out in his comment, with some noteworthy differences: 我使用的代码让人想起Haresh在评论中指出的内容,以及一些值得注意的差异:

  1. Haresh's code appears to assume that the camera was in portrait mode; Haresh的代码似乎假设相机处于纵向模式; mine aims to handle either case. 我的目标是处理这两种情况。

  2. Haresh's code does all the hard work explicitly on the main application thread, including the disk I/O, which is a very bad idea. Haresh的代码在主应用程序线程上明确地完成了所有的工作,包括磁盘I / O,这是一个非常糟糕的主意。 Mine is a background thread. 我是一个背景线程。

  3. His code downsamples the image. 他的代码对图像进行了下采样。 My guess is that this is to help prevent out-of-memory errors, which is definitely a challenge here. 我的猜测是,这有助于防止内存不足错误,这绝对是一个挑战。 In my case, I give the developers using my library the option of specifying a large heap, to help ensure that I can perform the bitmap rotation. 在我的例子中,我给开发人员使用我的库指定一个大堆的选项,以帮助确保我可以执行位图旋转。 Eventually, I will move this code to the NDK, where memory allocations are not counted against the Dalvik heap limit. 最后,我将此代码移动到NDK,其中内存分配不计入Dalvik堆限制。

  4. My code is also responsible for other device-specific issues, such as flipping the FFC image if needed or mirroring the FFC image if requested by the developer. 我的代码还负责其他特定于设备的问题,例如在需要时翻转FFC图像或在开发人员请求时镜像FFC图像。

However, my code is specifically designed to be used by my library, and so it would need a fair amount of work to break out the logic into something that could be used independently. 但是,我的代码专门设计为我的库使用,因此需要相当多的工作来将逻辑分解为可以独立使用的东西。

Note that the link to my code is specifically to v0.6.9 of the library. 请注意, 我的代码的链接专门针对库的v0.6.9。 Newer versions of this class may exist in the repo if you are reading this months after I wrote this answer. 如果您在写完这个答案后几个月正在阅读这个类的更新版本可能存在于回购中。 Conversely, the class may no longer exist in the repo master , though, if I have moved the code into the NDK. 相反,如果我已将代码移动到NDK中,则repo master可能不再存在该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM