public void addSample(String packagePath, Sample sample) { // convert something like 'org.controlsfx.samples.actions' to 'samples.actions' String packagesWithoutBase = ""; try { if (!basePackage.equals(packagePath)) { packagesWithoutBase = packagePath.substring(basePackage.length() + 1); } } catch (StringIndexOutOfBoundsException e) { System.out.println("packagePath: " + packagePath + ", basePackage: " + basePackage); e.printStackTrace(); return; } // then split up the packages into separate strings String[] packages = packagesWithoutBase.isEmpty() ? new String[] {} : packagesWithoutBase.split("\\."); // then for each package convert to a prettier form for (int i = 0; i < packages.length; i++) { String packageName = packages[i]; if (packageName.isEmpty()) continue; packageName = packageName.substring(0, 1).toUpperCase() + packageName.substring(1); packageName = packageName.replace("_", " "); packages[i] = packageName; } // now we have the pretty package names, we add this sample into the // tree in the appropriate place sampleTree.addSample(packages, sample); }
public static String getSentencecaseString(String name) { String titleCaseValue = null; try { String[] words = name.split(" "); StringBuilder sb = new StringBuilder(); if (words[0].length() > 0) { sb.append( Character.toUpperCase(words[0].charAt(0)) + words[0].subSequence(1, words[0].length()).toString().toLowerCase()); for (int i = 1; i < words.length; i++) { sb.append(" "); sb.append( Character.toUpperCase(words[i].charAt(0)) + words[i].subSequence(1, words[i].length()).toString().toLowerCase()); } } titleCaseValue = sb.toString(); // Log.i("STAG",titleCaseValue); } catch (StringIndexOutOfBoundsException e) { titleCaseValue = name; e.printStackTrace(); } catch (Exception e) { titleCaseValue = name; e.printStackTrace(); } return titleCaseValue; }
/** * Cut String by appointed size (Byte unit) * * @param beforeStr String need to convert * @param afterSize Size of string after converting(Byte Unit) * @return String String after converting */ public static String cnvStrFixedLength(String beforeStr, int afterSize) { if (beforeStr == null) { return ""; } else if (afterSize == 0) { return ""; } else { // String after converting String afterStr = null; try { byte[] beforStrByte = beforeStr.getBytes("Shift_JIS"); // In case string include only one-byte character(),and // string's size < appointed size if ((beforeStr.getBytes().length == beforeStr.length() && beforeStr.length() <= afterSize) || beforStrByte.length <= afterSize) { // Return same string return beforeStr; } else { // Generate after converting string(byte) byte[] afterStrByte = new byte[afterSize]; for (int i = 0; i < afterSize; i++) { afterStrByte[i] = beforStrByte[i]; } // Check if the last 2-byte character can be converted or // not try { // String tmpNewString = new String(afterStrByte); // If the last 2-byte character is not converted // correctly // Throw StringIndexOutOfBoundsException // String lastString = tmpNewString.substring(tmpNewString.length() - 1); afterStr = new String(afterStrByte); } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); // Remove the last 2-byte character byte[] tmpBytes = afterStrByte; afterStrByte = new byte[tmpBytes.length - 1]; for (int i = 0; i < afterStrByte.length; i++) { afterStrByte[i] = tmpBytes[i]; } afterStr = new StringBuffer(new String(afterStrByte, "Shift_JIS")).toString(); } } } catch (Exception e) { return null; } return afterStr; } }
@Override protected void connectedFunction() { int decimalCount = 0; int remainingCharacters = 3; byte[] rawPacket = new byte[100]; int read = 0; while (mState == BluetoothState.CONNECTED && remainingCharacters > 0) { // Read one byte try { read += mBluetoothSocket.getInputStream().read(rawPacket, read, 1); } catch (IOException e) { e.printStackTrace(); lostConnection(); break; } String temp = ""; try { temp = new String(rawPacket, read - 1, 1, "ISO-8859-1"); if (temp.equals(".")) { if (decimalCount <= MAX_DECIMALS) decimalCount++; else remainingCharacters = 0; } else { if (decimalCount == MAX_DECIMALS) remainingCharacters--; } } catch (UnsupportedEncodingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (StringIndexOutOfBoundsException e1) { e1.printStackTrace(); } } if (mState == BluetoothState.CONNECTED) { AffectivaPacket p = null; try { p = AffectivaPacket.packetFromString(new String(rawPacket, 0, read, "ISO-8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // If p is not null the packet is a valid affectiva packet if (p != null) { // Log.d(TAG,"Read: "+p.toString()); if (mHandler != null) mHandler.obtainMessage(SensorService.MESSAGE_BLUETOOTH_DATA, p).sendToTarget(); } else { Log.d(TAG, "Read: invalid packet"); } } }
public static void main(String args[]) { String textLine = new String(""); int i = 0; try { System.out.println("aData1.dat integrity check."); AtomTypeFile f = new AtomTypeFile(); // BufferedReader text = new BufferedReader(new FileReader("p.txt")); BufferedReader text = new BufferedReader( new FileReader(newNanocad.txtDir + newNanocad.fileSeparator + "p.txt")); while (text.ready()) { textLine = text.readLine(); String numText = textLine.substring(0, textLine.indexOf(" ")); int TextAtomNum = Integer.parseInt(numText); atomProperty curProp = f.getProperty(i); // System.out.println(curProp.getANumber() + " " + textLine); if (curProp.getANumber() != TextAtomNum) { System.out.print("ANumber mismatch for " + textLine + " (" + i + ")"); System.out.println(" - dat:" + curProp.getANumber() + " txt:" + TextAtomNum); while (curProp.getANumber() != TextAtomNum) { int j = 0; while (curProp.getANumber() > TextAtomNum) { textLine = text.readLine(); numText = textLine.substring(0, textLine.indexOf(" ")); TextAtomNum = Integer.parseInt(numText); j++; } while (curProp.getANumber() < TextAtomNum) { i++; curProp = f.getProperty(i); j++; } System.out.println(j + " items skipped."); } } i++; } } catch (StringIndexOutOfBoundsException e) { System.out.println("Text parse error in line " + i + ": " + textLine); e.printStackTrace(); } catch (Exception e) { System.out.println("ERROR"); System.out.println(i + ": " + textLine); e.printStackTrace(); } System.out.println("Integrity check concluded."); }
public static Pair makeJavaMethodPair(String s) { try { String left = s.substring(s.indexOf('{') + 1, s.indexOf('\t')); JavaMethod t1 = null; if (!left.equals("empty")) t1 = new JavaMethod(left); ; String right = s.substring(s.indexOf('\t') + 1, s.indexOf('}')); JavaMethod t2 = null; if (!right.equals("empty")) t2 = new JavaMethod(right); return new Pair(t1, t2); } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); System.out.println(s); } return null; }
/** * Rekursive Erzeugung möglicher Permuationen aus der Eckenliste. * * @param beginning Leere Liste * @param ending Eckenliste */ public void generatePermutation(List<Edge> beginning, List<Edge> ending) { if (ending.size() <= 1) { ArrayList<Edge> list = new ArrayList<Edge>(beginning); list.addAll(ending); permutations.add(new Path(list)); } else { for (int i = 0; i < ending.size(); i++) { try { List<Edge> newList = new ArrayList<Edge>(ending.subList(0, i)); newList.addAll(ending.subList(i + 1, ending.size())); List<Edge> newBeginning = new ArrayList<Edge>(beginning); newBeginning.add(ending.get(i)); generatePermutation(newBeginning, newList); } catch (StringIndexOutOfBoundsException exception) { exception.printStackTrace(); } } } }
/** @param args program arguments */ public static void main(String[] args) { AggressiveUrlCanonicalizer canonicalizer = new AggressiveUrlCanonicalizer(); int n = 0; int i = 0; ArrayList<Integer> columns = new ArrayList<Integer>(); long lineNumber = 0; boolean cdxPassThru = false; String delimiter = " "; while (n < args.length) { String arg = args[n]; if (arg.compareTo("-cdx") == 0) { cdxPassThru = true; n++; continue; } if (n == (args.length - 1)) { USAGE(); } String val = args[n + 1]; if (arg.compareTo("-f") == 0) { columns.add(new Integer(val)); } else if (arg.compareTo("-d") == 0) { delimiter = val; } else { USAGE(); } n += 2; } // place default '0' in case none specified: if (columns.size() == 0) { columns.add(new Integer(1)); } // convert to int[]: int[] cols = new int[columns.size()]; for (int idx = 0; idx < columns.size(); idx++) { cols[idx] = columns.get(idx).intValue() - 1; } BufferedReader r = new BufferedReader(new InputStreamReader(System.in, ByteOp.UTF8)); StringBuilder sb = new StringBuilder(); String line = null; while (true) { try { line = r.readLine(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } if (line == null) { break; } lineNumber++; if (cdxPassThru && line.startsWith(CDX_PREFIX)) { System.out.println(line); continue; } String parts[] = line.split(delimiter); for (int column : cols) { if (column >= parts.length) { System.err.println("Invalid line " + lineNumber + " (" + line + ") skipped"); } else { try { parts[column] = canonicalizer.urlStringToKey(parts[column]); } catch (URIException e) { System.err.println( "Invalid URL in line " + lineNumber + " (" + line + ") skipped (" + parts[column] + ")"); e.printStackTrace(); continue; } catch (StringIndexOutOfBoundsException e) { System.err.println( "Invalid URL in line " + lineNumber + " (" + line + ") skipped (" + parts[column] + ")"); e.printStackTrace(); continue; } } } sb.setLength(0); for (i = 0; i < parts.length; i++) { sb.append(parts[i]); if (i < (parts.length - 1)) { sb.append(delimiter); } } System.out.println(sb.toString()); } }
@SuppressWarnings("unused") @Override public void onDraw(Canvas canvas) { Rect frame = cameraManager.getFramingRect(); if (frame == null) { return; } int width = canvas.getWidth(); int height = canvas.getHeight(); paint.setColor(maskColor); canvas.drawRect(0, 0, width, frame.top, paint); canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); canvas.drawRect(0, frame.bottom + 1, width, height, paint); if (resultText != null) { Point bitmapSize = resultText.getBitmapDimensions(); previewFrame = cameraManager.getFramingRectInPreview(); if (bitmapSize.x == previewFrame.width() && bitmapSize.y == previewFrame.height()) { float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height(); if (DRAW_REGION_BOXES) { regionBoundingBoxes = resultText.getRegionBoundingBoxes(); for (int i = 0; i < regionBoundingBoxes.size(); i++) { paint.setAlpha(0xA0); paint.setColor(Color.MAGENTA); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); rect = regionBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_TEXTLINE_BOXES) { textlineBoundingBoxes = resultText.getTextlineBoundingBoxes(); paint.setAlpha(0xA0); paint.setColor(Color.RED); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < textlineBoundingBoxes.size(); i++) { rect = textlineBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_WORD_BOXES || DRAW_WORD_TEXT) { wordBoundingBoxes = resultText.getWordBoundingBoxes(); } if (DRAW_WORD_TEXT) { words = resultText.getText().replace("\n", " ").split(" "); int[] wordConfidences = resultText.getWordConfidences(); for (int i = 0; i < wordBoundingBoxes.size(); i++) { boolean isWordBlank = true; try { if (!words[i].equals("")) { isWordBlank = false; } } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } if (!isWordBlank) { rect = wordBoundingBoxes.get(i); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); if (DRAW_TRANSPARENT_WORD_BACKGROUNDS) { paint.setAlpha(wordConfidences[i] * 255 / 100); } else { paint.setAlpha(255); } canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); paint.setColor(Color.BLACK); paint.setAlpha(0xFF); paint.setAntiAlias(true); paint.setTextAlign(Align.LEFT); paint.setTextSize(100); paint.setTextScaleX(1.0f); Rect bounds = new Rect(); paint.getTextBounds(words[i], 0, words[i].length(), bounds); int h = bounds.bottom - bounds.top; float size = (((float) (rect.height()) / h) * 100f); paint.setTextSize(size); paint.setTextScaleX(1.0f); paint.getTextBounds(words[i], 0, words[i].length(), bounds); int w = bounds.right - bounds.left; int text_h = bounds.bottom - bounds.top; int baseline = bounds.bottom + ((rect.height() - text_h) / 2); float xscale = ((float) (rect.width())) / w; paint.setTextScaleX(xscale); canvas.drawText( words[i], frame.left + rect.left * scaleX, frame.top + rect.bottom * scaleY - baseline, paint); } } } if (DRAW_WORD_BOXES) { paint.setAlpha(0xA0); paint.setColor(0xFF00CCFF); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < wordBoundingBoxes.size(); i++) { rect = wordBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_CHARACTER_BOXES || DRAW_CHARACTER_TEXT) { characterBoundingBoxes = resultText.getCharacterBoundingBoxes(); } if (DRAW_CHARACTER_BOXES) { // Draw bounding boxes around each character paint.setAlpha(0xA0); paint.setColor(0xFF00FF00); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int c = 0; c < characterBoundingBoxes.size(); c++) { Rect characterRect = characterBoundingBoxes.get(c); canvas.drawRect( frame.left + characterRect.left * scaleX, frame.top + characterRect.top * scaleY, frame.left + characterRect.right * scaleX, frame.top + characterRect.bottom * scaleY, paint); } } if (DRAW_CHARACTER_TEXT) { // Draw letters individually for (int i = 0; i < characterBoundingBoxes.size(); i++) { Rect r = characterBoundingBoxes.get(i); // Draw a white background for every letter int meanConfidence = resultText.getMeanConfidence(); paint.setColor(Color.WHITE); paint.setAlpha(meanConfidence * (255 / 100)); paint.setStyle(Style.FILL); canvas.drawRect( frame.left + r.left * scaleX, frame.top + r.top * scaleY, frame.left + r.right * scaleX, frame.top + r.bottom * scaleY, paint); // Draw each letter, in black paint.setColor(Color.BLACK); paint.setAlpha(0xFF); paint.setAntiAlias(true); paint.setTextAlign(Align.LEFT); String letter = ""; try { char c = resultText.getText().replace("\n", "").replace(" ", "").charAt(i); letter = Character.toString(c); if (!letter.equals("-") && !letter.equals("_")) { // Adjust text size to fill rect paint.setTextSize(100); paint.setTextScaleX(1.0f); // ask the paint for the bounding rect if it were to draw this text Rect bounds = new Rect(); paint.getTextBounds(letter, 0, letter.length(), bounds); // get the height that would have been produced int h = bounds.bottom - bounds.top; // figure out what textSize setting would create that height of text float size = (((float) (r.height()) / h) * 100f); // and set it into the paint paint.setTextSize(size); // Draw the text as is. We don't really need to set the text scale, because the // dimensions // of the Rect should already be suited for drawing our letter. canvas.drawText( letter, frame.left + r.left * scaleX, frame.top + r.bottom * scaleY, paint); } } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } } } // Draw a two pixel solid border inside the framing rect paint.setAlpha(0); paint.setStyle(Style.FILL); paint.setColor(frameColor); canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint); canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint); canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint); canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint); // Draw the framing rect corner UI elements paint.setColor(cornerColor); canvas.drawRect(frame.left - 15, frame.top - 15, frame.left + 15, frame.top, paint); canvas.drawRect(frame.left - 15, frame.top, frame.left, frame.top + 15, paint); canvas.drawRect(frame.right - 15, frame.top - 15, frame.right + 15, frame.top, paint); canvas.drawRect(frame.right, frame.top - 15, frame.right + 15, frame.top + 15, paint); canvas.drawRect(frame.left - 15, frame.bottom, frame.left + 15, frame.bottom + 15, paint); canvas.drawRect(frame.left - 15, frame.bottom - 15, frame.left, frame.bottom, paint); canvas.drawRect(frame.right - 15, frame.bottom, frame.right + 15, frame.bottom + 15, paint); canvas.drawRect(frame.right, frame.bottom - 15, frame.right + 15, frame.bottom + 15, paint); }
/** * Operates on the invariant that for any sorted hand, if the 1st card is not a club, there are no * clubs in the hand, if the 2nd card is not a diamond there are no diamonds, etc. This allows * in-place char replacements (i.e. without swapping a->temp, b->a, temp->b). All hands are sorted * in their constructors * * @return a hand string suited to match the database's hand-saving hash of * clubs->diamonds->hearts->spades */ @SuppressWarnings("serial") public String toDatabaseString() { System.out.println("toDatabaseString() input: " + this.toString()); ArrayList<Character> suits = new ArrayList<Character>() { { add('c'); add('d'); add('h'); add('s'); } }; String ans = new String(this.toString()); int counter = 0; ArrayList<Card> seen = new ArrayList<Card>(); Card max = getMaxValueCard(); // seen.add(max); ans = ans.replace(max.getSuit().charAt(0), ' '); ans = ans.replace(suits.get(counter), max.getSuit().charAt(0)); ans = ans.replace(' ', suits.get(counter)); seen.add(new Card(max.getRank().charAt(0), suits.get(counter))); counter++; if (new Hand(ans).getMaxValueCard(seen) != null && new Hand(ans).getMaxValueCard(seen).getSuit().charAt(0) != seen.get(seen.size() - 1).getSuit().charAt(0)) { max = new Hand(ans).getMaxValueCard(seen); // seen.add(max); while (suits.indexOf(max.getSuit().charAt(0)) < counter) { seen.add(max); max = new Hand(ans).getMaxValueCard(seen); // seen.add(max); if (max == null) { ans = new Hand(ans).toString(); return ans; } } ans = ans.replace(max.getSuit().charAt(0), ' '); ans = ans.replace(suits.get(counter), max.getSuit().charAt(0)); ans = ans.replace(' ', suits.get(counter)); seen.add(new Card(max.getRank().charAt(0), suits.get(counter))); counter++; } else { max = new Hand(ans).getMaxValueCard(seen); seen.add(max); if (max == null) { ans = new Hand(ans).toString(); return ans; } } if (new Hand(ans).getMaxValueCard(seen) != null && new Hand(ans).getMaxValueCard(seen).getSuit().charAt(0) != seen.get(seen.size() - 1).getSuit().charAt(0)) { max = new Hand(ans).getMaxValueCard(seen); // seen.add(max); while (suits.indexOf(max.getSuit().charAt(0)) < counter) { seen.add(max); max = new Hand(ans).getMaxValueCard(seen); // seen.add(max); if (max == null) { ans = new Hand(ans).toString(); return ans; } } ans = ans.replace(max.getSuit().charAt(0), ' '); ans = ans.replace(suits.get(counter), max.getSuit().charAt(0)); ans = ans.replace(' ', suits.get(counter)); seen.add(new Card(max.getRank().charAt(0), suits.get(counter))); counter++; } else { max = new Hand(ans).getMaxValueCard(seen); seen.add(max); if (max == null) { ans = new Hand(ans).toString(); return ans; } } ans = new Hand(ans).toString(); System.out.println("Intermediary: " + ans); int suitTracker = 1; while (suitTracker < numDifferentSuits()) { boolean temp = true; for (int i = 1; i < ans.length(); i += 2) { if (ans.charAt(i) == suits.get(suitTracker)) { temp = false; } } if (temp) { int last_index = -1; for (int j = 1; j < ans.length(); j += 2) { if (ans.charAt(j) == suits.get(suitTracker - 1)) { last_index = j; } } try { ans = ans.replace(ans.charAt(last_index + 2), suits.get(suitTracker)); } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); } } suitTracker++; } ans = new Hand(ans).toString(); System.out.println("Intermediary2: " + ans); int max_flush = 0; char max_suit = ' '; for (char c : suits) { if (max_flush < get_num_suit(ans, c)) { max_flush = Math.max(max_flush, c); max_suit = c; } } ans = ans.replace(max_suit, ' '); ans = ans.replace('c', max_suit); ans = ans.replace(' ', 'c'); ans = new Hand(ans).toString(); System.out.println("Returning: " + ans); return ans; }
@Override public List<AndroidMethod> parse() throws IOException { BufferedReader rdr = null; List<AndroidMethod> resList = new ArrayList<AndroidMethod>(); try { rdr = new BufferedReader(new FileReader(this.fileName)); String line = null; boolean firstLine = true; while ((line = rdr.readLine()) != null) { // Ignore the first line which is a header if (firstLine) { firstLine = false; continue; } firstLine = false; // Get the CSV fields String[] fields = line.split("\t"); if (fields.length < 1) { System.err.println("Found invalid line: " + line); continue; } // Parse the method signature String methodName; String className; List<String> methodParams = new ArrayList<String>(); Set<String> permissions = new HashSet<String>(); try { if (fields[0].contains(")")) methodName = fields[0].substring(0, fields[0].indexOf("(")); else methodName = fields[0]; className = methodName.substring(0, methodName.lastIndexOf(".")); methodName = methodName.substring(methodName.lastIndexOf(".") + 1); // Parse the parameters if (fields[0].contains("(")) { String parameters = fields[0].substring(fields[0].indexOf("(") + 1); parameters = parameters.substring(0, parameters.indexOf(")")); for (String p : parameters.split(",")) methodParams.add(p); } String perm = (fields.length > 1) ? fields[1] : ""; perm = perm.replaceAll(" and ", " "); perm = perm.replaceAll(" or ", " "); if (perm.contains(".")) perm = perm.substring(perm.lastIndexOf(".") + 1); for (String p : perm.split(" ")) permissions.add(p); } catch (StringIndexOutOfBoundsException ex) { System.err.println("Could not parse line: " + line); ex.printStackTrace(); continue; } AndroidMethod method = new AndroidMethod(methodName, methodParams, "", className, permissions); resList.add(method); } } finally { if (rdr != null) rdr.close(); } return resList; }
@SuppressWarnings("unused") @Override public void onDraw(Canvas canvas) { Rect frame = cameraManager.getFramingRect(); if (frame == null) { return; } int width = canvas.getWidth(); int height = canvas.getHeight(); // Draw the exterior (i.e. outside the framing rect) darkened paint.setColor(maskColor); canvas.drawRect(0, 0, width, frame.top, paint); canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); canvas.drawRect(0, frame.bottom + 1, width, height, paint); // If we have an OCR result, overlay its information on the viewfinder. if (resultText != null) { // Only draw text/bounding boxes on viewfinder if it hasn't been resized since the OCR was // requested. Point bitmapSize = resultText.getBitmapDimensions(); previewFrame = cameraManager.getFramingRectInPreview(); if (bitmapSize.x == previewFrame.width() && bitmapSize.y == previewFrame.height()) { float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height(); if (DRAW_REGION_BOXES) { regionBoundingBoxes = resultText.getRegionBoundingBoxes(); for (int i = 0; i < regionBoundingBoxes.size(); i++) { paint.setAlpha(0xA0); paint.setColor(Color.MAGENTA); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); rect = regionBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_TEXTLINE_BOXES) { // Draw each textline textlineBoundingBoxes = resultText.getTextlineBoundingBoxes(); paint.setAlpha(0xA0); paint.setColor(Color.RED); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < textlineBoundingBoxes.size(); i++) { rect = textlineBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_STRIP_BOXES) { stripBoundingBoxes = resultText.getStripBoundingBoxes(); paint.setAlpha(0xFF); paint.setColor(Color.YELLOW); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < stripBoundingBoxes.size(); i++) { rect = stripBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_WORD_BOXES || DRAW_WORD_TEXT) { // Split the text into words wordBoundingBoxes = resultText.getWordBoundingBoxes(); // for (String w : words) { // Log.e("ViewfinderView", "word: " + w); // } // Log.d("ViewfinderView", "There are " + words.length + " words in the string array."); // Log.d("ViewfinderView", "There are " + wordBoundingBoxes.size() + " words with bounding // boxes."); } if (DRAW_WORD_BOXES) { paint.setAlpha(0xFF); paint.setColor(0xFF00CCFF); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int i = 0; i < wordBoundingBoxes.size(); i++) { // Draw a bounding box around the word rect = wordBoundingBoxes.get(i); canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); } } if (DRAW_WORD_TEXT) { words = resultText.getText().replace("\n", " ").split(" "); int[] wordConfidences = resultText.getWordConfidences(); for (int i = 0; i < wordBoundingBoxes.size(); i++) { boolean isWordBlank = true; try { if (!words[i].equals("")) { isWordBlank = false; } } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } // Only draw if word has characters if (!isWordBlank) { // Draw a white background around each word rect = wordBoundingBoxes.get(i); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); if (DRAW_TRANSPARENT_WORD_BACKGROUNDS) { // Higher confidence = more opaque, less transparent background paint.setAlpha(wordConfidences[i] * 255 / 100); } else { paint.setAlpha(255); } canvas.drawRect( frame.left + rect.left * scaleX, frame.top + rect.top * scaleY, frame.left + rect.right * scaleX, frame.top + rect.bottom * scaleY, paint); // Draw the word in black text paint.setColor(Color.BLACK); paint.setAlpha(0xFF); paint.setAntiAlias(true); paint.setTextAlign(Align.LEFT); // Adjust text size to fill rect paint.setTextSize(100); paint.setTextScaleX(1.0f); // ask the paint for the bounding rect if it were to draw this text Rect bounds = new Rect(); paint.getTextBounds(words[i], 0, words[i].length(), bounds); // get the height that would have been produced int h = bounds.bottom - bounds.top; // figure out what textSize setting would create that height of text float size = (((float) (rect.height()) / h) * 100f); // and set it into the paint paint.setTextSize(size); // Now set the scale. // do calculation with scale of 1.0 (no scale) paint.setTextScaleX(1.0f); // ask the paint for the bounding rect if it were to draw this text. paint.getTextBounds(words[i], 0, words[i].length(), bounds); // determine the width int w = bounds.right - bounds.left; // calculate the baseline to use so that the entire text is visible including the // descenders int text_h = bounds.bottom - bounds.top; int baseline = bounds.bottom + ((rect.height() - text_h) / 2); // determine how much to scale the width to fit the view float xscale = ((float) (rect.width())) / w; // set the scale for the text paint paint.setTextScaleX(xscale); canvas.drawText( words[i], frame.left + rect.left * scaleX, frame.top + rect.bottom * scaleY - baseline, paint); } } } if (DRAW_CHARACTER_BOXES || DRAW_CHARACTER_TEXT) { characterBoundingBoxes = resultText.getCharacterBoundingBoxes(); } if (DRAW_CHARACTER_BOXES) { // Draw bounding boxes around each character paint.setAlpha(0xA0); paint.setColor(0xFF00FF00); paint.setStyle(Style.STROKE); paint.setStrokeWidth(1); for (int c = 0; c < characterBoundingBoxes.size(); c++) { Rect characterRect = characterBoundingBoxes.get(c); canvas.drawRect( frame.left + characterRect.left * scaleX, frame.top + characterRect.top * scaleY, frame.left + characterRect.right * scaleX, frame.top + characterRect.bottom * scaleY, paint); } } if (DRAW_CHARACTER_TEXT) { // Draw letters individually for (int i = 0; i < characterBoundingBoxes.size(); i++) { Rect r = characterBoundingBoxes.get(i); // Draw a white background for every letter int meanConfidence = resultText.getMeanConfidence(); paint.setColor(Color.WHITE); paint.setAlpha(meanConfidence * (255 / 100)); paint.setStyle(Style.FILL); canvas.drawRect( frame.left + r.left * scaleX, frame.top + r.top * scaleY, frame.left + r.right * scaleX, frame.top + r.bottom * scaleY, paint); // Draw each letter, in black paint.setColor(Color.BLACK); paint.setAlpha(0xFF); paint.setAntiAlias(true); paint.setTextAlign(Align.LEFT); String letter = ""; try { char c = resultText.getText().replace("\n", "").replace(" ", "").charAt(i); letter = Character.toString(c); if (!letter.equals("-") && !letter.equals("_")) { // Adjust text size to fill rect paint.setTextSize(100); paint.setTextScaleX(1.0f); // ask the paint for the bounding rect if it were to draw this text Rect bounds = new Rect(); paint.getTextBounds(letter, 0, letter.length(), bounds); // get the height that would have been produced int h = bounds.bottom - bounds.top; // figure out what textSize setting would create that height of text float size = (((float) (r.height()) / h) * 100f); // and set it into the paint paint.setTextSize(size); // Draw the text as is. We don't really need to set the text scale, because the // dimensions // of the Rect should already be suited for drawing our letter. canvas.drawText( letter, frame.left + r.left * scaleX, frame.top + r.bottom * scaleY, paint); } } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } } } // Draw a two pixel solid border inside the framing rect paint.setAlpha(0); paint.setStyle(Style.FILL); paint.setColor(frameColor); canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint); canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint); canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint); canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint); // Draw the framing rect corner UI elements paint.setColor(cornerColor); // canvas.drawRect(frame.left - 15, frame.top - 15, frame.left + 15, frame.top, paint); // canvas.drawRect(frame.left - 15, frame.top, frame.left, frame.top + 15, paint); // canvas.drawRect(frame.right - 15, frame.top - 15, frame.right + 15, frame.top, paint); // canvas.drawRect(frame.right, frame.top - 15, frame.right + 15, frame.top + 15, paint); // canvas.drawRect(frame.left - 15, frame.bottom, frame.left + 15, frame.bottom + 15, paint); // canvas.drawRect(frame.left - 15, frame.bottom - 15, frame.left, frame.bottom, paint); // canvas.drawRect(frame.right - 15, frame.bottom, frame.right + 15, frame.bottom + 15, // paint); // canvas.drawRect(frame.right, frame.bottom - 15, frame.right + 15, frame.bottom + 15, // paint); // Request another update at the animation interval, but don't repaint the entire viewfinder // mask. // postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom); }