TARIQ ALEED
TARIQ ALEED

Reputation: 79

rotate the image 90 degrees

Is it possible to rotate the image 90 degrees

Note : the rotation is not in ImageView object , but in The original image in the path With many thanks

      public void Rotate90 (){ 

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

Upvotes: 0

Views: 3554

Answers (4)

Surendrareddy Tetala
Surendrareddy Tetala

Reputation: 11

<!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?

Upvotes: 1

Jaura
Jaura

Reputation: 487

1) You have to get the bitmap from your path like this:

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

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

2) Pass myBitmap to this function for getting 90 degrees rotated image

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);
}

Upvotes: 0

Ahmed Basyouny
Ahmed Basyouny

Reputation: 343

first load your image from file

Bitmap bitmap = BitmapFactory.decodeFile(image Path);

then make a matrix

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();
    }
}

and don't forget to add permission

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

hope this help you :)

Upvotes: 2

Related Questions