public class ImageUtil {
public static final String TAG = ImageUtil.class.getSimpleName();
/**
* 判断图片是否需要翻转(正方向的不用翻转)
*
* @param fileName
* @return
*/
public static boolean needRotate(Context context, String fileName) {
try {
ExifInterface exif = new ExifInterface(fileName);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
LogUtil.d(TAG, "orientation is " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return true;
case ExifInterface.ORIENTATION_ROTATE_270:
return true;
case ExifInterface.ORIENTATION_ROTATE_180:
return true;
}
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 获取翻转到正方向的的图片
*
* @param fileName
* @param maxWidth
* @param maxHeight
* @return
*/
public static void rotatedBitmap(Context context, String fileName,
String dstFile) {
try {
ExifInterface exif = new ExifInterface(fileName);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
LogUtil.d(TAG, "orientation is " + orientation);
Matrix matrix = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix = new Matrix();
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix = new Matrix();
matrix.postRotate(270);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix = new Matrix();
matrix.postRotate(180);
break;
}
Bitmap bmp = getSmallBitmap(context,
Uri.fromFile(new File(fileName)));
if (matrix != null) {
Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
bmp.recycle();
FileOutputStream fos = new FileOutputStream(dstFile);
bmp2.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
bmp2.recycle();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean compress(Context context, Uri src, File dst, int rate) {
try {
Bitmap bmp = getSmallBitmap(context, src);
FileOutputStream fos = new FileOutputStream(dst);
bmp.compress(Bitmap.CompressFormat.JPEG, rate, fos);
bmp.recycle();
bmp = null;
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap getSmallBitmap(Context context, Uri uri) {
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, options);
is.close();
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 720, 1280);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
is = context.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
is.close();
return bmp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
glLoadIdentity()
将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作
1.X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。
2.OpenGL屏幕中心的坐标值是X和Y轴上的0.0f点。
3.中心左面的坐标值是负值,右面是正值。
移向屏幕顶端是正值,移向屏幕底端是负值。
移入屏幕深处是负值,移出屏幕则是正值。
glTranslatef(x, y, z)
沿着 X, Y 和 Z 轴移动。
注意在glTranslatef(x, y, z)中,当您移动的时候,您并不是相对屏幕中心移动,而是相对与当前所在的屏幕位置。其作用就是将你绘点坐标的原点在当前原点的基础上平移一个(x,y,z)向量。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
/////////////////////////////////////////////////////////////////////////////////////
程序的运行结果如下:
左边的三角形是第一步绘制的,可以看到该三角形绘制的坐标系,实际上是以(-1.5f,0.0f,-6.0f)为原点的。
第二个三角形绘制的时候,由于使用glLoadIdentity()使原点重新回到屏幕中心来,因此其原点位于屏幕的中心。
glRotatef(angle, x, y, z)
与glTranslatef(x, y, z)类似,glRotatef(angle, x, y, z)也是对坐标系进行操作。
旋转轴经过原点,方向为(x,y,z),旋转角度为angle,方向满足右手定则。
////////////////////////////////////////////////////////////////
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
////////////////////////////////////////////////////////////////
在未旋转的情况下如图所示:
////////////////////////////////////////////////////////////////
glLoadIdentity();
glRotatef(45,0.0f,0.0f,1.0f);
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
////////////////////////////////////////////////////////////////
绕Z轴正向旋转45度角,因为Z轴正方向由屏幕内指向屏幕外,由右手定则可知方向为逆时针转动。
由于直角顶点即为原点,因此将围绕直角逆时针旋转。
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_QUADS);
glVertex3f(-0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f, 0.01f, 0.0f);
glVertex3f(-0.01f, 0.01f, 0.0f);
glEnd();
glLoadIdentity();
// glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(1.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_QUADS);
glVertex3f(-0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f, 0.01f, 0.0f);
glVertex3f(-0.01f, 0.01f, 0.0f);
glEnd();
glLoadIdentity();
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(1.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_QUADS);
glVertex3f(-0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f, 0.01f, 0.0f);
glVertex3f(-0.01f, 0.01f, 0.0f);
glEnd();
glLoadIdentity();
glTranslatef(1.0f,0.0f,-6.0f);
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
////////////////////////////////////////////////////////////////
glLoadIdentity();
glRotatef(45,0.0f,0.0f,1.0f);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
////////////////////////////////////////////////////////////////
在旋转之后加了一个复位的指令,图形就不会旋转了。
见附件: