Skip to content

Instantly share code, notes, and snippets.

@itoz
Created March 24, 2011 03:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itoz/884499 to your computer and use it in GitHub Desktop.
Save itoz/884499 to your computer and use it in GitHub Desktop.
Eixf の Orientation(回転情報)から、正の位置に補正したBitmapDataを返す.
/**
* EixfのOrientation(回転情報)から、正の位置に補正したBitmapDataを返す.
* @param bitmapData 補正前のBitmapData
* @param exifOrient exifのOrient情報 1~8
* @see http://sylvana.net/jpegcrop/exif_orientation.html
*/
public static function correctExifOrient(bitmapData : BitmapData, exifOrient : int) : BitmapData
{
var correctBMD : BitmapData;
var mat : Matrix = new Matrix();
switch(exifOrient) {
case 2:
// trace(exifOrient + " : 左右反転している");
correctBMD = new BitmapData(bitmapData.width, bitmapData.height);
mat.a = -1;
mat.translate(bitmapData.width, 0);
break;
case 3:
// trace(exifOrient + " : 180°回転している");
correctBMD = new BitmapData(bitmapData.width, bitmapData.height);
mat.rotate(180 * (Math.PI / 180));
mat.translate(bitmapData.width, bitmapData.height);
break;
case 4:
// trace(exifOrient + " : 上下反転している");
correctBMD = new BitmapData(bitmapData.width, bitmapData.height);
mat.d = -1;
mat.translate(0, bitmapData.height);
break;
case 5:
// trace(exifOrient + " : 上下反転 > 時計回りに90°回転している ");
correctBMD = new BitmapData(bitmapData.height, bitmapData.width);
mat.a = -1;
mat.rotate(-90 * (Math.PI / 180));
break;
case 6:
// trace(exifOrient + " : 反時計回りに90°回転している");
correctBMD = new BitmapData(bitmapData.height, bitmapData.width);
mat.rotate(90 * (Math.PI / 180));
mat.translate(bitmapData.height, 0);
break;
case 7:
// trace(exifOrient + " : 上下反転 > 反時計回りに90°回転 している");
correctBMD = new BitmapData(bitmapData.height, bitmapData.width);
mat.d = -1;
mat.rotate(-90 * (Math.PI / 180));
mat.translate(bitmapData.height, bitmapData.width);
break;
case 8:
// trace(exifOrient + " : 時計回りに90°回転している");
correctBMD = new BitmapData(bitmapData.height, bitmapData.width);
mat.rotate(-90 * (Math.PI / 180));
mat.translate(0, bitmapData.width);
break;
default :
// trace(exifOrient + " :通常 or 回転情報がありません");
correctBMD = new BitmapData(bitmapData.width, bitmapData.height);
break;
}
correctBMD.draw(bitmapData, mat);
return correctBMD;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment