【问题标题】:Android SDK Nexus S native camera issueAndroid SDK Nexus S 原生相机问题
【发布时间】:2011-09-19 16:10:23
【问题描述】:

我正在使用 android 原生相机开发一个应用程序。我可以成功拍照并在 ImageView 上存储和显示。我正在测试 HTC 欲望 Z、Nexus S 和摩托罗拉 Zoom。除了 Nexus S 上的一个问题外,它适用于所有三种设备。当我使用 Nexus S 拍照时,预览图像已旋转 90 度

您能告诉我如何解决这个问题吗?

我的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_preview);
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView);
        getPhotoClick();
    }
    private File getTempFile()
    {
        return new File(Environment.getExternalStorageDirectory(),  "image.tmp");
    }

    private void getPhotoClick()
    {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));
      startActivityForResult(intent, REQUEST_FROM_CAMERA);
    }

    InputStream is=null;

    @Override
    protected void onActivityResult(int requestcode, int resultCode, Intent data){
        if (requestcode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {

            File file=getTempFile();

            try {
                is=new FileInputStream(file);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if(is==null){

                try {
                    Uri u = data.getData();
                    is=getContentResolver().openInputStream(u);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

            Bitmap thumbnail = BitmapFactory.decodeStream(is);
            imgTakenPhoto.setImageBitmap(thumbnail);

          }
    } 

我得到了Problems saving a photo to a file的帮助

非常感谢

有人吗?

【问题讨论】:

    标签: android orientation android-camera nexus-s android-camera-intent


    【解决方案1】:

    此预览问题出现在某些手机上。我知道的唯一简单的解决方法是在 onCreate() 中设置横向模式:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    另一种(可能是矫枉过正)的可能性是在预览表面上覆盖另一个表面,创建自定义 PreviewCallback 并手动旋转和绘制每一帧到这个表面,但是在性能和​​屏幕尺寸方面存在问题。

    【讨论】:

    • 如果您创建自己的相机界面,这将有效。如果您使用的是本机相机,则此方法不起作用。我找到了解决方案。我稍后会发布它。干杯
    • @Chinthaka 有没有可以在这里发布的解决方案?
    【解决方案2】:

    以下函数将检测图像的方向。

    public static int getExifOrientation(String filepath) {
            int degree = 0;
            ExifInterface exif = null;
            try {
                exif = new ExifInterface(filepath);
            } catch (IOException ex) {
                Log.e(TAG, "cannot read exif", ex);
            }
            if (exif != null) {
                int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION, -1);
                if (orientation != -1) {                    
                    switch(orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            degree = 90;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            degree = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            degree = 270;
                            break;
                 }
        }
    }
    

    在您的代码中,使用此函数来旋转位图图像。 Util类可以在https://android.googlesource.com/platform/packages/apps/Camera.git找到

    if(degree != 0){
      bmp = Util.rotate(bmp, degree);
    }
    

    请记住,这只会更正图像预览的方向。 我希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      相关资源
      最近更新 更多