00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00114 private Rect previewFrame;
00115 private Rect rect;
00116
00117
00118 public ViewfinderView(Context context, AttributeSet attrs) {
00119 super(context, attrs);
00120
00121
00122
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
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
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
00157 if (resultText != null) {
00158
00159
00160
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
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
00218 wordBoundingBoxes = resultText.getWordBoundingBoxes();
00219
00220
00221
00222
00223
00224
00225
00226
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
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
00258 if (!isWordBlank) {
00259
00260 rect = wordBoundingBoxes.get(i);
00261 paint.setColor(Color.WHITE);
00262 paint.setStyle(Style.FILL);
00263 if (DRAW_TRANSPARENT_WORD_BACKGROUNDS) {
00264
00265
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
00276 paint.setColor(Color.BLACK);
00277 paint.setAlpha(0xFF);
00278 paint.setAntiAlias(true);
00279 paint.setTextAlign(Align.LEFT);
00280
00281
00282 paint.setTextSize(100);
00283 paint.setTextScaleX(1.0f);
00284
00285
00286 Rect bounds = new Rect();
00287 paint.getTextBounds(words[i], 0, words[i].length(),
00288 bounds);
00289
00290 int h = bounds.bottom - bounds.top;
00291
00292
00293 float size = (((float) (rect.height()) / h) * 100f);
00294
00295 paint.setTextSize(size);
00296
00297
00298 paint.setTextScaleX(1.0f);
00299
00300
00301 paint.getTextBounds(words[i], 0, words[i].length(),
00302 bounds);
00303
00304 int w = bounds.right - bounds.left;
00305
00306
00307 int text_h = bounds.bottom - bounds.top;
00308 int baseline = bounds.bottom
00309 + ((rect.height() - text_h) / 2);
00310
00311
00312 float xscale = ((float) (rect.width())) / w;
00313
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
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
00346 for (int i = 0; i < characterBoundingBoxes.size(); i++) {
00347 Rect r = characterBoundingBoxes.get(i);
00348
00349
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
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
00373 paint.setTextSize(100);
00374 paint.setTextScaleX(1.0f);
00375
00376
00377
00378 Rect bounds = new Rect();
00379 paint.getTextBounds(letter, 0, letter.length(),
00380 bounds);
00381
00382
00383 int h = bounds.bottom - bounds.top;
00384
00385
00386
00387 float size = (((float) (r.height()) / h) * 100f);
00388
00389
00390 paint.setTextSize(size);
00391
00392
00393
00394
00395
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
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
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
00443
00444
00445
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 }