旋转图像左/右,因为我们可以在画廊做和保存 [英] Rotate image left/right as we can do in gallery and save

查看:186
本文介绍了旋转图像左/右,因为我们可以在画廊做和保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转图像左/右,因为我们可以在画廊做,保存,问题是我失去质量,当我打开画廊,旋转图像显示原始的缩略图(不旋转),当我打开图像,然后我看原图(不旋转)和秒库加载后旋转的图像。

在理想情况下,应该如何发生的,你可以检查您的画廊,尝试旋转图像,你可以看到,即使在缩略图得到适当的旋转。

总之我要旋转图像左/右,我们在画廊看到。
请做要紧。

我试过很多解决方案

我所做的就是

当用户点击转动我的应用程序左/右图标我执行这个code

 私人无效writeRotatedBitmap(浮点角){    尝试{        档案文件=新的文件(sheetPaths.get(计数));        BMP位图= BitmapFactory.de codeFILE(sheetPaths.get(计数));        字模=新的Matrix();
        matrix.postRotate(角度);
        BMP = Bitmap.createBitmap(BMP,0,0,bmp.getWidth(),bmp.getHeight(),矩阵,真);        FileOutputStream中出;
        OUT =新的FileOutputStream(文件);
        bmp.com preSS(Bitmap.Com pressFormat.JPG,90出);
        了out.flush();
        out.close();        BMP = NULL;    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
}


解决方案

@ user2094075:请下载混帐用于测试目的

 位图mBitmap; //初始化

//对于移位rotateLeft

  mBitmap = Util.rotateImage(mBitmap,-90);
                    RotateBitmap rotateBitmap =新RotateBitmap(mBitmap);
                    mImageView.setImageRotateBitma presetBase(rotateBitmap,真);

//对于移位rotateRight

  mBitmap = Util.rotateImage(mBitmap,90);
                    RotateBitmap rotateBitmap =新RotateBitmap(mBitmap);
                    mImageView.setImageRotateBitma presetBase(rotateBitmap,真);

的另一种方法

//对于移位rotateRight

  Your_Image_View.setImageBitmap(rotateImage(BitmapFactory.de codeResource(getResources(),R.drawable.image),90));

//对于移位rotateLeft **

  Your_Image_View.setImageBitmap(rotateImage(BitmapFactory.de codeResource(getResources(),R.drawable.image), -  90));

然后

 公共位图rotateImage(位图SRC,浮度){
     //创建新的Matrix对象
     字模=新的Matrix();
     //设置旋转角度
     matrix.postRotate(度);
     //返回利用矩阵新位图旋转
     返回Bitmap.createBitmap(源,0,0,src.getWidth(),src.getHeight(),矩阵,真);
 }

I am trying to rotate image left/right as we can do in gallery and saving it, problem is I am losing the quality and when I open the gallery, the thumbnail of rotated image show original(not rotated) and when I open the image then I see original image (not rotated) and after a seconds gallery loads the rotated image.

Ideally how it should happen, you can check that in your gallery, try rotated an image, you can see even the thumbnail get rotated properly.

In short I want to rotate image left/right as we see in gallery. Please do the needful.

I tried many solutions

What I have done is

When user clicks rotate left/right icon in my app I execute this code

private void writeRotatedBitmap(float angle) {

    try {

        File file = new File(sheetPaths.get(count));

        Bitmap bmp = BitmapFactory.decodeFile(sheetPaths.get(count));

        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        bmp =  Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

        FileOutputStream out;
        out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPG, 90, out);
        out.flush();
        out.close();

        bmp = null;

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

解决方案

@user2094075 : Please download this git for testing purpose

Bitmap          mBitmap; // Initialize 

// For rotate rotateLeft

mBitmap = Util.rotateImage(mBitmap, -90);
                    RotateBitmap rotateBitmap = new RotateBitmap(mBitmap);
                    mImageView.setImageRotateBitmapResetBase(rotateBitmap, true);

// For rotate rotateRight

 mBitmap = Util.rotateImage(mBitmap, 90);
                    RotateBitmap rotateBitmap = new RotateBitmap(mBitmap);
                    mImageView.setImageRotateBitmapResetBase(rotateBitmap, true);

Another way

// For rotate rotateRight

Your_Image_View.setImageBitmap(rotateImage(BitmapFactory.decodeResource(getResources(), R.drawable.image),90));

// For rotate rotateLeft**

Your_Image_View.setImageBitmap(rotateImage(BitmapFactory.decodeResource(getResources(), R.drawable.image),-90));

And then

    public Bitmap rotateImage(Bitmap src, float degree) {
     // create new matrix object
     Matrix matrix = new Matrix();
     // setup rotation degree
     matrix.postRotate(degree);
     // return new bitmap rotated using matrix
     return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
 }

这篇关于旋转图像左/右,因为我们可以在画廊做和保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆