forked from MMP-forTour/cropimage
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Util.cs
156 lines (136 loc) · 4.95 KB
/
Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Android.Graphics;
using System;
namespace CropImage
{
public class Util
{
// Rotates the bitmap by the specified degree.
// If a new bitmap is created, the original bitmap is recycled.
public static Bitmap rotateImage(Bitmap b, int degrees)
{
if (degrees != 0 && b != null)
{
Matrix m = new Matrix();
m.SetRotate(degrees,
(float)b.Width / 2, (float)b.Height / 2);
try
{
Bitmap b2 = Bitmap.CreateBitmap(
b, 0, 0, b.Width, b.Height, m, true);
if (b != b2)
{
b.Recycle();
b = b2;
}
}
catch (Java.Lang.OutOfMemoryError)
{
// We have no memory to rotate. Return the original bitmap.
}
}
return b;
}
public static Bitmap transform(Matrix scaler,
Bitmap source,
int targetWidth,
int targetHeight,
bool scaleUp)
{
int deltaX = source.Width - targetWidth;
int deltaY = source.Height - targetHeight;
if (!scaleUp && (deltaX < 0 || deltaY < 0))
{
// In this case the bitmap is smaller, at least in one dimension,
// than the target. Transform it by placing as much of the image
// as possible into the target and leaving the top/bottom or
// left/right (or both) black.
Bitmap b2 = Bitmap.CreateBitmap(targetWidth, targetHeight,
Bitmap.Config.Argb8888);
Canvas c = new Canvas(b2);
int deltaXHalf = Math.Max(0, deltaX / 2);
int deltaYHalf = Math.Max(0, deltaY / 2);
Rect src = new Rect(
deltaXHalf,
deltaYHalf,
deltaXHalf + Math.Min(targetWidth, source.Width),
deltaYHalf + Math.Min(targetHeight, source.Height));
int dstX = (targetWidth - src.Width()) / 2;
int dstY = (targetHeight - src.Height()) / 2;
Rect dst = new Rect(
dstX,
dstY,
targetWidth - dstX,
targetHeight - dstY);
c.DrawBitmap(source, src, dst, null);
return b2;
}
float bitmapWidthF = source.Width;
float bitmapHeightF = source.Height;
float bitmapAspect = bitmapWidthF / bitmapHeightF;
float viewAspect = (float)targetWidth / targetHeight;
if (bitmapAspect > viewAspect)
{
float scale = targetHeight / bitmapHeightF;
if (scale < .9F || scale > 1F)
{
scaler.SetScale(scale, scale);
}
else
{
scaler = null;
}
}
else
{
float scale = targetWidth / bitmapWidthF;
if (scale < .9F || scale > 1F)
{
scaler.SetScale(scale, scale);
}
else
{
scaler = null;
}
}
Bitmap b1;
if (scaler != null)
{
// this is used for minithumb and crop, so we want to filter here.
b1 = Bitmap.CreateBitmap(source, 0, 0,
source.Width, source.Height, scaler, true);
}
else
{
b1 = source;
}
int dx1 = Math.Max(0, b1.Width - targetWidth);
int dy1 = Math.Max(0, b1.Height - targetHeight);
Bitmap b3 = Bitmap.CreateBitmap(
b1,
dx1 / 2,
dy1 / 2,
targetWidth,
targetHeight);
if (b1 != source)
{
b1.Recycle();
}
return b3;
}
}
}