Повернуть изображение на 90 градусов
Можно ли повернуть изображение на 90 градусов?
Примечание: вращение не в ImageView
объект, но в оригинальном изображении в пути С большим спасибо
public void Rotate90 (){
// Rotate90 this File image1= new File(Environment.getExternalStorageDirectory().getPath() + "/0Students/Compressed_Images/t12.jpg" );
}
4 ответа
Сначала загрузите ваше изображение из файла
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" />
надеюсь, это поможет вам:)
<!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?
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);
}
Проверить с помощью setRotation (float) API
http://developer.android.com/reference/android/view/View.html