【问题标题】:rotate the image 90 degrees将图像旋转 90 度
【发布时间】:2015-07-07 11:29:44
【问题描述】:

图片可以旋转90度

注意:旋转不在ImageView对象中,而是在路径中的原图 非常感谢

      public void Rotate90 (){ 

//      Rotate90 this       File image1= new File(Environment.getExternalStorageDirectory().getPath() + "/0Students/Compressed_Images/t12.jpg" );
    }

【问题讨论】:

  • 你的意思是从文件中旋转图像并再次保存??
  • 您的意思是从文件中旋转图像并再次保存吗?是的,感谢您的回复

标签: android image rotation 360-degrees


【解决方案1】:

首先从文件中加载你的图片

Bitmap bitmap = BitmapFactory.decodeFile(image Path);

然后做一个矩阵

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap finalBitmap = Bitmap.createBitmap(bitmap , 0, 0,
                bitmap .getWidth(), bitmap .getHeight(),
                matrix, true);

then save it to your system 



   private void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

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

别忘了添加权限

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

希望对你有帮助:)

【讨论】:

  • 感谢您的帮助,我真的很感激,但是压缩功能会改变图像分辨率甚至到 100%,我希望得到另一种没有压缩功能的方式与最美好的问候
  • 不幸的是,据我所知,没有其他办法,但如果你不想失去质量,请替换这个 finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);用这个 finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);由于PNG是无损的,请参考以下链接groups.google.com/forum/#!topic/android-developers/4q3lxOlZKRQ
【解决方案2】:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
      <link rel="stylesheet" href="styles.css">
      <script src="javascript.js"></script>
  </head>
  <body>
    <img id="kitten" class="" src="http://makeameme.org/media/templates/120/grumpy_cat.jpg" alt="" width="120" height="120">
  </body>
</html>
javascript.js:

let image = document.querySelectorAll("#kitten")
image.onclick = toggleClass;

function toggleClass(){
  if(image.className == 'image'){
      image.className = ''
  } else {
      image.className = 'image'
  }
}

styles.CSS:
#kitten {

    position: absolute;
    top:10%;
    left:60%;
    width:120px;
    height:120px;
    margin: -60px 0 0 -60px;
}

.image{
    -webkit-animation:spin 4s linear infinite;
    -moz-animation:spin 4s linear infinite;
    animation:spin 4s linear infinite;
}

@-moz-keyframes spin {100% { -moz-transform: rotate(360deg);} }
@-webkit-keyframes spin {100% { -webkit-transform: rotate(360deg);} }
@keyframes spin {100% { -webkit-transform: rotate(360deg); transform:rotate(360deg);} }

 still my image is not rotating after clicking on image can any solve it?

【讨论】:

  • 欢迎来到 StackOverflow,在回答问题时,请务必提供对代码的描述,这可能对 OP 有帮助,而不仅仅是简单的代码,因为他会理解它为什么会这样工作。
【解决方案3】:

【讨论】:

  • 感谢您的回复,没有工作函数的例子
【解决方案4】:

1) 你必须像这样从你的路径中获取位图:

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
}

2) 将 myBitmap 传递给此函数以获取旋转 90 度的图像

public static Bitmap RotateBitmap(Bitmap source, float angle) {
  Matrix matrix = new Matrix();
  matrix.postRotate(angle);
  return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2013-02-13
    • 2013-04-22
    • 2012-05-05
    相关资源
    最近更新 更多