简体   繁体   English

捕获的图像始终显示为风景,并且setRotate无法解析

[英]Captured image always shows landscape and setRotate cannot be resolved

There are quite a few similar topics and issues around here and I follow this . 有相当多的类似的题目和在这里的问题,我跟着这个 But I get error. 但是我得到了错误。

My Code 我的密码

@Override

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

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            //h=0;
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
                    //pic = photo;
                    break;
                }
            }

            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmapOptions.inJustDecodeBounds = false;
                bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                bitmapOptions.inDither = true;
                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                Global.img = bitmap;

                b.setImageBitmap(bitmap);
                String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
                //p = path;
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {

                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    //pic=file;
                    outFile.flush();
                    outFile.close();


                } catch (FileNotFoundException e) {
                    e.printStackTrace();

                } catch (IOException e) {
                    e.printStackTrace();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();

            }

        } else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            // h=1;
            //imgui = selectedImage;
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
            Log.w("path of image ******", picturePath + "");
            b.setImageBitmap(thumbnail);
        }


    }
    else
    {
        finish();
    }

}

After follow the tutorial, I have changed my code to 遵循本教程后,我将代码更改为

  try {
             Bitmap bitmap;
             BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
             bitmapOptions.inJustDecodeBounds = false;
             bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
             bitmapOptions.inDither = true;
             bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
              BitmapFactory.Options opts = new BitmapFactory.Options();
              Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath(), opts);
              ExifInterface exif = new ExifInterface(f.getAbsolutePath());
              String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
              int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;
               int rotationAngle = 0;
               if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
               if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
               if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
               Matrix matrix = new Matrix();
               matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
               Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);
               Global.img = bitmap;
               b.setImageBitmap(bitmap);
               String path = android.os.Environment
                          .getExternalStorageDirectory()
                           + File.separator
                           + "Phoenix" + File.separator + "default";
                    //p = path;

                    f.delete();

                    OutputStream outFile = null;

                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
  1. setRotate in matrix.setRotate cannot be solved. 无法解析matrix.setRotate中的setRotate。
  2. I get a red line underneath (bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true); 我在下面有一条红线(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true); (cannot resolved method createBitmap ) (无法解决方法createBitmap)

*********Edit********* *********编辑*********

After I import android.graphics.Matrix instead of android.opengl.Matrix, the app crashed. 在我导入android.graphics.Matrix而不是android.opengl.Matrix,该应用程序崩溃了。

LogCat error LogCat错误

  Process: com.example.project.project, PID: 13045
    java.lang.OutOfMemoryError
            at android.graphics.Bitmap.nativeCreate(Native Method)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:833)
            at com.example.project.project.ImageFitScreen.onActivityResult(ImageFitScreen.java:236)
            at android.app.Activity.dispatchActivityResult(Activity.java:5643)

This is line 236 这是第236行

 Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);

更改为此位图rotationBitmap = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight(),matrix,true);

我猜您导入了错误的Matrix类,您应该导入android.graphics.Matrix,而不是android.opengl.Matrix,请仔细检查。

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

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