不幸的是,当在android中通过横向模式捕获图像时,图像未旋转已停止?

Posted

技术标签:

【中文标题】不幸的是,当在android中通过横向模式捕获图像时,图像未旋转已停止?【英文标题】:Unfortunately, Image not roted has stopped, when image capture via landscape mode in android? 【发布时间】:2014-07-11 16:56:55 【问题描述】:

我的代码是: MainActivity.java:

public class MainActivity extends ActionBarActivity 
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 0;
    public String imageName = null, imagePath;
    public ImageView imageView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        SecureRandom random = new SecureRandom();
        String randomName = new BigInteger(10, random).toString(4);
        imageName = "myImage" + "" + randomName + ".JPEG";
        File makeFile = new File(Environment.getExternalStorageDirectory()
                + "/" + "imageFolderRoted");
        if (!makeFile.exists()) 
            if (makeFile.mkdir()) 

            
        
        File file = new File(makeFile, imageName);

        imagePath = file.getAbsolutePath();
        Uri outputFileUri = Uri.fromFile(file);
        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(i, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) 
            if (resultCode == RESULT_OK) 
                Matrix matrix = new Matrix();
                matrix.postRotate(getImageOrientation(imagePath));
                Bitmap bitmap = getPreview(imagePath);
                Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                imageView1.setImageBitmap(rotatedBitmap);
             else if (resultCode == RESULT_CANCELED) 
                // user cancelled Image capture

            
        
    

    public static int getImageOrientation(String imagePath) 
        int rotate = 0;
        try 

            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) 
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            
         catch (IOException e) 
            e.printStackTrace();
        
        return rotate;
    

    public Bitmap getPreview(String fileName) 
        File image = new File(fileName);

        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(image.getPath(), bounds);
        if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) 
            return null;
        
        int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
                : bounds.outWidth;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 8;
        return BitmapFactory.decodeFile(image.getPath(), opts);
    

在 AndroidMenifest.xml 中添加权限:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

当运行(横向)然后应用程序崩溃。并且还显示不幸的是,Image not roted 已停止。

如何成功地从横向模式捕获图像。

[N.B] : 在设备三星 s4 中运行

然后显示 logcat 崩溃信息:

AndroidRuntime(4002): FATAL EXCEPTION: main
05-25 10:53:14.232: E/AndroidRuntime(4002): java.lang.RuntimeException: Unable to resume activity com.example.imagenotroted/com.example.imagenotroted.MainActivity: java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=0, result=-1, data=null to activity com.example.imagenotroted/com.example.imagenotroted.MainActivity: java.lang.NullPointerException
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2595)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2623)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2109)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3510)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.os.Looper.loop(Looper.java:154)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.main(ActivityThread.java:4624)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at dalvik.system.NativeStart.main(Native Method)
05-25 10:53:14.232: E/AndroidRuntime(4002): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=0, result=-1, data=null to activity com.example.imagenotroted/com.example.imagenotroted.MainActivity: java.lang.NullPointerException
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3135)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2578)
05-25 10:53:14.232: E/AndroidRuntime(4002):     ... 13 more
05-25 10:53:14.232: E/AndroidRuntime(4002): Caused by: java.lang.NullPointerException
05-25 10:53:14.232: E/AndroidRuntime(4002):     at com.example.imagenotroted.MainActivity.onActivityResult(MainActivity.java:63)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.Activity.dispatchActivityResult(Activity.java:4730)
05-25 10:53:14.232: E/AndroidRuntime(4002):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)
05-25 10:53:14.232: E/AndroidRuntime(4002):     ... 14 more
05-25 10:53:15.279: E/ActivityManager(234): mtprof entry can not found!
05-25 10:53:15.279: E/ActivityManager(234): java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)
05-25 10:53:15.279: E/ActivityManager(234):     at libcore.io.IoBridge.open(IoBridge.java:448)
05-25 10:53:15.279: E/ActivityManager(234):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-25 10:53:15.279: E/ActivityManager(234):     at java.io.FileInputStream.<init>(FileInputStream.java:105)
05-25 10:53:15.279: E/ActivityManager(234):     at com.android.server.am.ActivityRecord.mtProf(ActivityRecord.java:872)
05-25 10:53:15.279: E/ActivityManager(234):     at com.android.server.am.ActivityRecord.windowsDrawn(ActivityRecord.java:662)
05-25 10:53:15.279: E/ActivityManager(234):     at com.android.server.am.ActivityRecord$Token.windowsDrawn(ActivityRecord.java:225)
05-25 10:53:15.279: E/ActivityManager(234):     at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7056)
05-25 10:53:15.279: E/ActivityManager(234):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 10:53:15.279: E/ActivityManager(234):     at android.os.Looper.loop(Looper.java:154)
05-25 10:53:15.279: E/ActivityManager(234):     at com.android.server.wm.WindowManagerService$WMThread.run(WindowManagerService.java:758)
05-25 10:53:15.279: E/ActivityManager(234): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-25 10:53:15.279: E/ActivityManager(234):     at libcore.io.Posix.open(Native Method)
05-25 10:53:15.279: E/ActivityManager(234):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-25 10:53:15.279: E/ActivityManager(234):     at libcore.io.IoBridge.open(IoBridge.java:432)
05-25 10:53:15.279: E/ActivityManager(234):     ... 9 more
05-25 10:53:19.025: E/Activity(3652): zbx The class is: android.process.media

【问题讨论】:

Android Camera Intent Saving Image Landscape When Taken Portrait 的可能重复项 能否请您发布 logcat 以便我们查看崩溃的位置? 【参考方案1】:

您的问题可能在下面的链接中解决

Captured Photo orientation is changing in android

希望您能从此链接获得帮助。

【讨论】:

以上是关于不幸的是,当在android中通过横向模式捕获图像时,图像未旋转已停止?的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中使用相机捕获自定义图像大小?

在 Python 中通过 win32com 使用 COM 对象

直接在内部存储中捕获图像

修复横向模式下的旋转问题

图像以横向模式拉伸

Android CameraX 图像旋转