Home | History | Annotate | Download | only in imageshow
      1 /*
      2  * Copyright (C) 2013 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.gallery3d.filtershow.imageshow;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.graphics.Matrix;
     23 import android.graphics.Paint;
     24 import android.graphics.Paint.Style;
     25 import android.graphics.RectF;
     26 import android.view.MotionEvent;
     27 
     28 import com.android.gallery3d.filtershow.filters.FilterPoint;
     29 import com.android.gallery3d.filtershow.filters.RedEyeCandidate;
     30 
     31 public class ImageRedEye extends ImagePoint {
     32     private static final String LOGTAG = "ImageRedEyes";
     33     private RectF mCurrentRect = null;
     34 
     35     public ImageRedEye(Context context) {
     36         super(context);
     37     }
     38 
     39     @Override
     40     public void resetParameter() {
     41         super.resetParameter();
     42         invalidate();
     43     }
     44 
     45     @Override
     46 
     47     public boolean onTouchEvent(MotionEvent event) {
     48         super.onTouchEvent(event);
     49 
     50         if (event.getPointerCount() > 1) {
     51             return true;
     52         }
     53 
     54         if (didFinishScalingOperation()) {
     55             return true;
     56         }
     57 
     58         float ex = event.getX();
     59         float ey = event.getY();
     60 
     61         // let's transform (ex, ey) to displayed image coordinates
     62         if (event.getAction() == MotionEvent.ACTION_DOWN) {
     63             mCurrentRect = new RectF();
     64             mCurrentRect.left = ex - mTouchPadding;
     65             mCurrentRect.top = ey - mTouchPadding;
     66         }
     67         if (event.getAction() == MotionEvent.ACTION_MOVE) {
     68             mCurrentRect.right = ex + mTouchPadding;
     69             mCurrentRect.bottom = ey + mTouchPadding;
     70         }
     71         if (event.getAction() == MotionEvent.ACTION_UP) {
     72             if (mCurrentRect != null) {
     73                 // transform to original coordinates
     74                 Matrix originalNoRotateToScreen = getImageToScreenMatrix(false);
     75                 Matrix originalToScreen = getImageToScreenMatrix(true);
     76                 Matrix invert = new Matrix();
     77                 originalToScreen.invert(invert);
     78                 RectF r = new RectF(mCurrentRect);
     79                 invert.mapRect(r);
     80                 RectF r2 = new RectF(mCurrentRect);
     81                 invert.reset();
     82                 originalNoRotateToScreen.invert(invert);
     83                 invert.mapRect(r2);
     84                 mRedEyeRep.addRect(r, r2);
     85                 this.resetImageCaches(this);
     86             }
     87             mCurrentRect = null;
     88         }
     89         mEditorRedEye.commitLocalRepresentation();
     90         invalidate();
     91         return true;
     92     }
     93 
     94     @Override
     95     public void onDraw(Canvas canvas) {
     96         super.onDraw(canvas);
     97         Paint paint = new Paint();
     98         paint.setStyle(Style.STROKE);
     99         paint.setColor(Color.RED);
    100         paint.setStrokeWidth(2);
    101         if (mCurrentRect != null) {
    102             paint.setColor(Color.RED);
    103             RectF drawRect = new RectF(mCurrentRect);
    104             canvas.drawRect(drawRect, paint);
    105         }
    106     }
    107 
    108     @Override
    109     protected void drawPoint(FilterPoint point, Canvas canvas, Matrix originalToScreen,
    110             Matrix originalRotateToScreen, Paint paint) {
    111         RedEyeCandidate candidate = (RedEyeCandidate) point;
    112         RectF rect = candidate.getRect();
    113         RectF drawRect = new RectF();
    114         originalToScreen.mapRect(drawRect, rect);
    115         RectF fullRect = new RectF();
    116         originalRotateToScreen.mapRect(fullRect, rect);
    117         paint.setColor(Color.BLUE);
    118         canvas.drawRect(fullRect, paint);
    119         canvas.drawLine(fullRect.centerX(), fullRect.top,
    120                 fullRect.centerX(), fullRect.bottom, paint);
    121         canvas.drawLine(fullRect.left, fullRect.centerY(),
    122                 fullRect.right, fullRect.centerY(), paint);
    123         paint.setColor(Color.GREEN);
    124         float dw = drawRect.width();
    125         float dh = drawRect.height();
    126         float dx = fullRect.centerX() - dw / 2;
    127         float dy = fullRect.centerY() - dh / 2;
    128         drawRect.set(dx, dy, dx + dw, dy + dh);
    129         canvas.drawRect(drawRect, paint);
    130         canvas.drawLine(drawRect.centerX(), drawRect.top,
    131                 drawRect.centerX(), drawRect.bottom, paint);
    132         canvas.drawLine(drawRect.left, drawRect.centerY(),
    133                 drawRect.right, drawRect.centerY(), paint);
    134         canvas.drawCircle(drawRect.centerX(), drawRect.centerY(),
    135                 mTouchPadding, paint);
    136     }
    137 }
    138