1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.photoeditor.filters; 18 19 import android.graphics.Canvas; 20 import android.graphics.Matrix; 21 import android.graphics.Paint; 22 import android.graphics.RectF; 23 24 import com.android.photoeditor.Photo; 25 import com.android.photoeditor.RectUtils; 26 27 /** 28 * Straighten filter applied to the image. 29 */ 30 public class StraightenFilter extends Filter { 31 32 private final RectF bounds = new RectF(); 33 private final Matrix matrix = new Matrix(); 34 private final Canvas canvas = new Canvas(); 35 private final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); 36 37 private float angle; 38 39 public void setAngle(float degrees) { 40 this.angle = degrees; 41 validate(); 42 } 43 44 @Override 45 public void process(Photo src, Photo dst) { 46 bounds.set(0, 0, src.width(), src.height()); 47 RectUtils.getStraightenMatrix(bounds, angle, matrix); 48 matrix.mapRect(bounds); 49 matrix.postTranslate((src.width() - bounds.width()) / 2, 50 (src.height() - bounds.height()) / 2); 51 canvas.setBitmap(dst.bitmap()); 52 canvas.drawBitmap(src.bitmap(), matrix, paint); 53 } 54 } 55