OCR Configurable  1.0
 Todo Clases Namespaces Archivos Funciones Variables
ViewfinderView.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright (C) 2008 ZXing authors
00003  * Copyright 2011 Robert Theis
00004  * Copyright 2012 Jaime Navarro Santapau
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00026 package edu.uoc.ocr;
00027 
00028 import java.util.List;
00029 
00030 import edu.uoc.ocr.R;
00031 import edu.uoc.ocr.camera.CameraManager;
00032 
00033 import android.content.Context;
00034 import android.content.res.Resources;
00035 import android.graphics.Canvas;
00036 import android.graphics.Color;
00037 import android.graphics.Paint;
00038 import android.graphics.Paint.Align;
00039 import android.graphics.Point;
00040 import android.graphics.Rect;
00041 import android.graphics.Paint.Style;
00042 import android.util.AttributeSet;
00043 import android.view.View;
00044 
00051 public final class ViewfinderView extends View {
00052 
00057         static final boolean DRAW_REGION_BOXES = false;
00058 
00063         static final boolean DRAW_TEXTLINE_BOXES = true;
00064 
00069         static final boolean DRAW_STRIP_BOXES = false;
00070 
00075         static final boolean DRAW_WORD_BOXES = true;
00076 
00081         static final boolean DRAW_TRANSPARENT_WORD_BACKGROUNDS = false;
00082 
00087         static final boolean DRAW_CHARACTER_BOXES = false;
00088 
00093         static final boolean DRAW_WORD_TEXT = false;
00094 
00099         static final boolean DRAW_CHARACTER_TEXT = false;
00100 
00101         private CameraManager cameraManager;
00102         private final Paint paint;
00103         private final int maskColor;
00104         private final int frameColor;
00105         private final int cornerColor;
00106         private OcrResultText resultText;
00107         private String[] words;
00108         private List<Rect> regionBoundingBoxes;
00109         private List<Rect> textlineBoundingBoxes;
00110         private List<Rect> stripBoundingBoxes;
00111         private List<Rect> wordBoundingBoxes;
00112         private List<Rect> characterBoundingBoxes;
00113         // Rect bounds;
00114         private Rect previewFrame;
00115         private Rect rect;
00116 
00117         // This constructor is used when the class is built from an XML resource.
00118         public ViewfinderView(Context context, AttributeSet attrs) {
00119                 super(context, attrs);
00120 
00121                 // Initialize these once for performance rather than calling them every
00122                 // time in onDraw().
00123                 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
00124                 Resources resources = getResources();
00125                 maskColor = resources.getColor(R.color.viewfinder_mask);
00126                 frameColor = resources.getColor(R.color.viewfinder_frame);
00127                 cornerColor = resources.getColor(R.color.viewfinder_corners);
00128 
00129                 // bounds = new Rect();
00130                 previewFrame = new Rect();
00131                 rect = new Rect();
00132         }
00133 
00134         public void setCameraManager(CameraManager cameraManager) {
00135                 this.cameraManager = cameraManager;
00136         }
00137 
00138         @SuppressWarnings("unused")
00139         @Override
00140         public void onDraw(Canvas canvas) {
00141                 Rect frame = cameraManager.getFramingRect();
00142                 if (frame == null) {
00143                         return;
00144                 }
00145                 int width = canvas.getWidth();
00146                 int height = canvas.getHeight();
00147 
00148                 // Draw the exterior (i.e. outside the framing rect) darkened
00149                 paint.setColor(maskColor);
00150                 canvas.drawRect(0, 0, width, frame.top, paint);
00151                 canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
00152                 canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
00153                                 paint);
00154                 canvas.drawRect(0, frame.bottom + 1, width, height, paint);
00155 
00156                 // If we have an OCR result, overlay its information on the viewfinder.
00157                 if (resultText != null) {
00158 
00159                         // Only draw text/bounding boxes on viewfinder if it hasn't been
00160                         // resized since the OCR was requested.
00161                         Point bitmapSize = resultText.getBitmapDimensions();
00162                         previewFrame = cameraManager.getFramingRectInPreview();
00163                         if (bitmapSize.x == previewFrame.width()
00164                                         && bitmapSize.y == previewFrame.height()) {
00165 
00166                                 float scaleX = frame.width() / (float) previewFrame.width();
00167                                 float scaleY = frame.height() / (float) previewFrame.height();
00168 
00169                                 if (DRAW_REGION_BOXES) {
00170                                         regionBoundingBoxes = resultText.getRegionBoundingBoxes();
00171                                         for (int i = 0; i < regionBoundingBoxes.size(); i++) {
00172                                                 paint.setAlpha(0xA0);
00173                                                 paint.setColor(Color.MAGENTA);
00174                                                 paint.setStyle(Style.STROKE);
00175                                                 paint.setStrokeWidth(1);
00176                                                 rect = regionBoundingBoxes.get(i);
00177                                                 canvas.drawRect(frame.left + rect.left * scaleX,
00178                                                                 frame.top + rect.top * scaleY, frame.left
00179                                                                                 + rect.right * scaleX, frame.top
00180                                                                                 + rect.bottom * scaleY, paint);
00181                                         }
00182                                 }
00183 
00184                                 if (DRAW_TEXTLINE_BOXES) {
00185                                         // Draw each textline
00186                                         textlineBoundingBoxes = resultText
00187                                                         .getTextlineBoundingBoxes();
00188                                         paint.setAlpha(0xA0);
00189                                         paint.setColor(Color.RED);
00190                                         paint.setStyle(Style.STROKE);
00191                                         paint.setStrokeWidth(1);
00192                                         for (int i = 0; i < textlineBoundingBoxes.size(); i++) {
00193                                                 rect = textlineBoundingBoxes.get(i);
00194                                                 canvas.drawRect(frame.left + rect.left * scaleX,
00195                                                                 frame.top + rect.top * scaleY, frame.left
00196                                                                                 + rect.right * scaleX, frame.top
00197                                                                                 + rect.bottom * scaleY, paint);
00198                                         }
00199                                 }
00200 
00201                                 if (DRAW_STRIP_BOXES) {
00202                                         stripBoundingBoxes = resultText.getStripBoundingBoxes();
00203                                         paint.setAlpha(0xFF);
00204                                         paint.setColor(Color.YELLOW);
00205                                         paint.setStyle(Style.STROKE);
00206                                         paint.setStrokeWidth(1);
00207                                         for (int i = 0; i < stripBoundingBoxes.size(); i++) {
00208                                                 rect = stripBoundingBoxes.get(i);
00209                                                 canvas.drawRect(frame.left + rect.left * scaleX,
00210                                                                 frame.top + rect.top * scaleY, frame.left
00211                                                                                 + rect.right * scaleX, frame.top
00212                                                                                 + rect.bottom * scaleY, paint);
00213                                         }
00214                                 }
00215 
00216                                 if (DRAW_WORD_BOXES || DRAW_WORD_TEXT) {
00217                                         // Split the text into words
00218                                         wordBoundingBoxes = resultText.getWordBoundingBoxes();
00219                                         // for (String w : words) {
00220                                         // Log.e("ViewfinderView", "word: " + w);
00221                                         // }
00222                                         // Log.d("ViewfinderView", "There are " + words.length +
00223                                         // " words in the string array.");
00224                                         // Log.d("ViewfinderView", "There are " +
00225                                         // wordBoundingBoxes.size() +
00226                                         // " words with bounding boxes.");
00227                                 }
00228 
00229                                 if (DRAW_WORD_BOXES) {
00230                                         paint.setAlpha(0xFF);
00231                                         paint.setColor(0xFF00CCFF);
00232                                         paint.setStyle(Style.STROKE);
00233                                         paint.setStrokeWidth(1);
00234                                         for (int i = 0; i < wordBoundingBoxes.size(); i++) {
00235                                                 // Draw a bounding box around the word
00236                                                 rect = wordBoundingBoxes.get(i);
00237                                                 canvas.drawRect(frame.left + rect.left * scaleX,
00238                                                                 frame.top + rect.top * scaleY, frame.left
00239                                                                                 + rect.right * scaleX, frame.top
00240                                                                                 + rect.bottom * scaleY, paint);
00241                                         }
00242                                 }
00243 
00244                                 if (DRAW_WORD_TEXT) {
00245                                         words = resultText.getText().replace("\n", " ").split(" ");
00246                                         int[] wordConfidences = resultText.getWordConfidences();
00247                                         for (int i = 0; i < wordBoundingBoxes.size(); i++) {
00248                                                 boolean isWordBlank = true;
00249                                                 try {
00250                                                         if (!words[i].equals("")) {
00251                                                                 isWordBlank = false;
00252                                                         }
00253                                                 } catch (ArrayIndexOutOfBoundsException e) {
00254                                                         e.printStackTrace();
00255                                                 }
00256 
00257                                                 // Only draw if word has characters
00258                                                 if (!isWordBlank) {
00259                                                         // Draw a white background around each word
00260                                                         rect = wordBoundingBoxes.get(i);
00261                                                         paint.setColor(Color.WHITE);
00262                                                         paint.setStyle(Style.FILL);
00263                                                         if (DRAW_TRANSPARENT_WORD_BACKGROUNDS) {
00264                                                                 // Higher confidence = more opaque, less
00265                                                                 // transparent background
00266                                                                 paint.setAlpha(wordConfidences[i] * 255 / 100);
00267                                                         } else {
00268                                                                 paint.setAlpha(255);
00269                                                         }
00270                                                         canvas.drawRect(frame.left + rect.left * scaleX,
00271                                                                         frame.top + rect.top * scaleY, frame.left
00272                                                                                         + rect.right * scaleX, frame.top
00273                                                                                         + rect.bottom * scaleY, paint);
00274 
00275                                                         // Draw the word in black text
00276                                                         paint.setColor(Color.BLACK);
00277                                                         paint.setAlpha(0xFF);
00278                                                         paint.setAntiAlias(true);
00279                                                         paint.setTextAlign(Align.LEFT);
00280 
00281                                                         // Adjust text size to fill rect
00282                                                         paint.setTextSize(100);
00283                                                         paint.setTextScaleX(1.0f);
00284                                                         // ask the paint for the bounding rect if it were to
00285                                                         // draw this text
00286                                                         Rect bounds = new Rect();
00287                                                         paint.getTextBounds(words[i], 0, words[i].length(),
00288                                                                         bounds);
00289                                                         // get the height that would have been produced
00290                                                         int h = bounds.bottom - bounds.top;
00291                                                         // figure out what textSize setting would create
00292                                                         // that height of text
00293                                                         float size = (((float) (rect.height()) / h) * 100f);
00294                                                         // and set it into the paint
00295                                                         paint.setTextSize(size);
00296                                                         // Now set the scale.
00297                                                         // do calculation with scale of 1.0 (no scale)
00298                                                         paint.setTextScaleX(1.0f);
00299                                                         // ask the paint for the bounding rect if it were to
00300                                                         // draw this text.
00301                                                         paint.getTextBounds(words[i], 0, words[i].length(),
00302                                                                         bounds);
00303                                                         // determine the width
00304                                                         int w = bounds.right - bounds.left;
00305                                                         // calculate the baseline to use so that the entire
00306                                                         // text is visible including the descenders
00307                                                         int text_h = bounds.bottom - bounds.top;
00308                                                         int baseline = bounds.bottom
00309                                                                         + ((rect.height() - text_h) / 2);
00310                                                         // determine how much to scale the width to fit the
00311                                                         // view
00312                                                         float xscale = ((float) (rect.width())) / w;
00313                                                         // set the scale for the text paint
00314                                                         paint.setTextScaleX(xscale);
00315                                                         canvas.drawText(words[i], frame.left + rect.left
00316                                                                         * scaleX, frame.top + rect.bottom * scaleY
00317                                                                         - baseline, paint);
00318                                                 }
00319 
00320                                         }
00321                                 }
00322 
00323                                 if (DRAW_CHARACTER_BOXES || DRAW_CHARACTER_TEXT) {
00324                                         characterBoundingBoxes = resultText
00325                                                         .getCharacterBoundingBoxes();
00326                                 }
00327 
00328                                 if (DRAW_CHARACTER_BOXES) {
00329                                         // Draw bounding boxes around each character
00330                                         paint.setAlpha(0xA0);
00331                                         paint.setColor(0xFF00FF00);
00332                                         paint.setStyle(Style.STROKE);
00333                                         paint.setStrokeWidth(1);
00334                                         for (int c = 0; c < characterBoundingBoxes.size(); c++) {
00335                                                 Rect characterRect = characterBoundingBoxes.get(c);
00336                                                 canvas.drawRect(frame.left + characterRect.left
00337                                                                 * scaleX, frame.top + characterRect.top
00338                                                                 * scaleY, frame.left + characterRect.right
00339                                                                 * scaleX, frame.top + characterRect.bottom
00340                                                                 * scaleY, paint);
00341                                         }
00342                                 }
00343 
00344                                 if (DRAW_CHARACTER_TEXT) {
00345                                         // Draw letters individually
00346                                         for (int i = 0; i < characterBoundingBoxes.size(); i++) {
00347                                                 Rect r = characterBoundingBoxes.get(i);
00348 
00349                                                 // Draw a white background for every letter
00350                                                 int meanConfidence = resultText.getMeanConfidence();
00351                                                 paint.setColor(Color.WHITE);
00352                                                 paint.setAlpha(meanConfidence * (255 / 100));
00353                                                 paint.setStyle(Style.FILL);
00354                                                 canvas.drawRect(frame.left + r.left * scaleX, frame.top
00355                                                                 + r.top * scaleY,
00356                                                                 frame.left + r.right * scaleX, frame.top
00357                                                                                 + r.bottom * scaleY, paint);
00358 
00359                                                 // Draw each letter, in black
00360                                                 paint.setColor(Color.BLACK);
00361                                                 paint.setAlpha(0xFF);
00362                                                 paint.setAntiAlias(true);
00363                                                 paint.setTextAlign(Align.LEFT);
00364                                                 String letter = "";
00365                                                 try {
00366                                                         char c = resultText.getText().replace("\n", "")
00367                                                                         .replace(" ", "").charAt(i);
00368                                                         letter = Character.toString(c);
00369 
00370                                                         if (!letter.equals("-") && !letter.equals("_")) {
00371 
00372                                                                 // Adjust text size to fill rect
00373                                                                 paint.setTextSize(100);
00374                                                                 paint.setTextScaleX(1.0f);
00375 
00376                                                                 // ask the paint for the bounding rect if it
00377                                                                 // were to draw this text
00378                                                                 Rect bounds = new Rect();
00379                                                                 paint.getTextBounds(letter, 0, letter.length(),
00380                                                                                 bounds);
00381 
00382                                                                 // get the height that would have been produced
00383                                                                 int h = bounds.bottom - bounds.top;
00384 
00385                                                                 // figure out what textSize setting would create
00386                                                                 // that height of text
00387                                                                 float size = (((float) (r.height()) / h) * 100f);
00388 
00389                                                                 // and set it into the paint
00390                                                                 paint.setTextSize(size);
00391 
00392                                                                 // Draw the text as is. We don't really need to
00393                                                                 // set the text scale, because the dimensions
00394                                                                 // of the Rect should already be suited for
00395                                                                 // drawing our letter.
00396                                                                 canvas.drawText(letter, frame.left + r.left
00397                                                                                 * scaleX,
00398                                                                                 frame.top + r.bottom * scaleY, paint);
00399                                                         }
00400                                                 } catch (StringIndexOutOfBoundsException e) {
00401                                                         e.printStackTrace();
00402                                                 } catch (Exception e) {
00403                                                         e.printStackTrace();
00404                                                 }
00405                                         }
00406                                 }
00407                         }
00408 
00409                 }
00410                 // Draw a two pixel solid border inside the framing rect
00411                 paint.setAlpha(0);
00412                 paint.setStyle(Style.FILL);
00413                 paint.setColor(frameColor);
00414                 canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2,
00415                                 paint);
00416                 canvas.drawRect(frame.left, frame.top + 2, frame.left + 2,
00417                                 frame.bottom - 1, paint);
00418                 canvas.drawRect(frame.right - 1, frame.top, frame.right + 1,
00419                                 frame.bottom - 1, paint);
00420                 canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1,
00421                                 frame.bottom + 1, paint);
00422 
00423                 // Draw the framing rect corner UI elements
00424                 paint.setColor(cornerColor);
00425                 canvas.drawRect(frame.left - 15, frame.top - 15, frame.left + 15,
00426                                 frame.top, paint);
00427                 canvas.drawRect(frame.left - 15, frame.top, frame.left, frame.top + 15,
00428                                 paint);
00429                 canvas.drawRect(frame.right - 15, frame.top - 15, frame.right + 15,
00430                                 frame.top, paint);
00431                 canvas.drawRect(frame.right, frame.top - 15, frame.right + 15,
00432                                 frame.top + 15, paint);
00433                 canvas.drawRect(frame.left - 15, frame.bottom, frame.left + 15,
00434                                 frame.bottom + 15, paint);
00435                 canvas.drawRect(frame.left - 15, frame.bottom - 15, frame.left,
00436                                 frame.bottom, paint);
00437                 canvas.drawRect(frame.right - 15, frame.bottom, frame.right + 15,
00438                                 frame.bottom + 15, paint);
00439                 canvas.drawRect(frame.right, frame.bottom - 15, frame.right + 15,
00440                                 frame.bottom + 15, paint);
00441 
00442                 // Request another update at the animation interval, but don't repaint
00443                 // the entire viewfinder mask.
00444                 // postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top,
00445                 // frame.right, frame.bottom);
00446         }
00447 
00448         public void drawViewfinder() {
00449                 invalidate();
00450         }
00451 
00458         public void addResultText(OcrResultText text) {
00459                 resultText = text;
00460         }
00461 
00465         public void removeResultText() {
00466                 resultText = null;
00467         }
00468 }
 Todo Clases Namespaces Archivos Funciones Variables