Site icon C1CTech

Recognize text in images with ML Kit on Android

&NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> &NewLine;<div>This post is about&comma; how to recognize text in images using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Google ML Kit Text Recognition API<&sol;strong><&sol;span> in Android application with the help of a simple demo app&period;<&sol;div> &NewLine;<div>In the Demo App&comma; we will recognize and extract text from images placed in the app assets and draw the bounding box around each element &lpar;word&rpar;&period;<&sol;div> &NewLine;<&sol;div> &NewLine;<div> <&sol;div> &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;13025e&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;TextRecognitionExp"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"3MCerJYS0aI" title&equals;"Recognize text in images with ML Kit on Android"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;3MCerJYS0aI"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;3MCerJYS0aI&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Recognize text in images with ML Kit on Android"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<div> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>ML Kit’s Text Recognition API<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>ML Kit’s Text Recognition API recognize and extract text from images&period; The Text Recognizer segments the text into <span style&equals;"color&colon; &num;0000ff&semi;"><strong>blocks &lpar; paragraph or column &rpar;&comma; lines&comma; and elements&lpar; word &rpar;<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Android Project<&sol;strong><&sol;span>&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty<&sol;strong><&sol;span> Activity&comma; provide app name&comma; select language to <span style&equals;"color&colon; &num;008000&semi;"><strong>java<&sol;strong><&sol;span> and then finally click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Adding Dependency<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>Open build&period;gradle &lpar;Module&colon; app&rpar; file and add the ML Kit’s Text Recognition API dependency inside the dependencies section&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> implementation 'com&period;google&period;android&period;gms&colon;play-services-mlkit-text-recognition&colon;16&period;0&period;0'<&sol;span><&sol;strong><br &sol;>&rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Layout File<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>The<strong><span style&equals;"color&colon; &num;008000&semi;"> activity&lowbar;main&period;xml<&sol;span><&sol;strong> layout file defines the UI of the application&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;ImageView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;image&lowbar;view"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"0dp"<br &sol;> android&colon;scaleType&equals;"fitStart"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;btn&lowbar;find&lowbar;text"<br &sol;> app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;com&period;c1ctech&period;textrecognitionexp&period;GraphicOverlay<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;graphic&lowbar;overlay"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"0dp"<br &sol;> android&colon;layout&lowbar;alignParentStart&equals;"true"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"&commat;id&sol;image&lowbar;view"<br &sol;> app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"&commat;id&sol;image&lowbar;view"<br &sol;> app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"&commat;id&sol;image&lowbar;view"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"&commat;id&sol;image&lowbar;view" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;find&lowbar;text"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;text&equals;"Find Text"<br &sol;> android&colon;layout&lowbar;margin&equals;"10dp"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<&sol;div> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Recognize and extract text from image bitmap<&sol;span><&sol;strong><&sol;h5> &NewLine;<p>To recognize and extract text from the image bitmap&comma; we have to follow the below steps&colon; <&sol;p> &NewLine;<ul> &NewLine;<li>prepare input image using a bitmap&period;<&sol;li> &NewLine;<li>creating TextRecognizer instance&period;<&sol;li> &NewLine;<li>process the image&period; &NewLine;<ul> &NewLine;<li>If the text recognition operation succeeds&comma; a <span style&equals;"color&colon; &num;008000&semi;"><strong>Text<&sol;strong><&sol;span> object is passed to the success listener&period;<&sol;li> &NewLine;<li>In case of error&comma; an <span style&equals;"color&colon; &num;008000&semi;"><strong>exception<&sol;strong><&sol;span> is passed to the failure listener&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;recognize and extract text from image bitmap<&sol;strong><&sol;span><br &sol;>private void runTextRecognition&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;prepare input image using bitmap<&sol;span><&sol;strong><br &sol;> InputImage image &equals; InputImage&period;fromBitmap&lpar;mSelectedImage&comma; 0&rpar;&semi;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;creating TextRecognizer instance<&sol;strong><&sol;span><br &sol;> TextRecognizer recognizer &equals; TextRecognition&period;getClient&lpar;&rpar;&semi;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;process the image<&sol;span><&sol;strong><br &sol;> recognizer&period;process&lpar;image&rpar;<br &sol;> &period;addOnSuccessListener&lpar;<br &sol;> new OnSuccessListener&lt&semi;Text&gt&semi;&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onSuccess&lpar;Text texts&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Task completed successfully<&sol;span><&sol;strong><br &sol;> processTextRecognitionResult&lpar;texts&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> &period;addOnFailureListener&lpar;<br &sol;> new OnFailureListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onFailure&lpar;&commat;NonNull Exception e&rpar; &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Task failed with an exception<&sol;strong><&sol;span><br &sol;> e&period;printStackTrace&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;&semi;<br &sol;>&rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Extract text from blocks of recognized text<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>A <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Text<&sol;strong><&sol;span> object contains the full text recognized in the image and zero or more TextBlock objects&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">TextBlock<&sol;span><&sol;strong> &&num;8211&semi;  represents a rectangular block of text&comma; which contains zero or more Line objects&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Line<&sol;span><&sol;strong> &&num;8211&semi;  object contains zero or more Element objects&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Element<&sol;span><&sol;strong> &&num;8211&semi;  objects which represent words and word-like entities such as dates and numbers&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre>private void processTextRecognitionResult&lpar;Text texts&rpar; &lbrace;<br &sol;> List&lt&semi;Text&period;TextBlock&gt&semi; blocks &equals; texts&period;getTextBlocks&lpar;&rpar;&semi;<br &sol;> if &lpar;blocks&period;size&lpar;&rpar; &equals;&equals; 0&rpar; &lbrace;<br &sol;> Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "No text found"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;<br &sol;> return&semi;<br &sol;> &rcub;<br &sol;> mGraphicOverlay&period;clear&lpar;&rpar;&semi;<br &sol;> for &lpar;Text&period;TextBlock block &colon; texts&period;getTextBlocks&lpar;&rpar;&rpar; &lbrace;<br &sol;> for &lpar;Text&period;Line line &colon; block&period;getLines&lpar;&rpar;&rpar; &lbrace;<br &sol;> for &lpar;Text&period;Element element &colon; line&period;getElements&lpar;&rpar;&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;draws the bounding box around the element&period;<&sol;span><&sol;strong><br &sol;> GraphicOverlay&period;Graphic textGraphic &equals; new TextGraphic&lpar;mGraphicOverlay&comma; element&rpar;&semi;<br &sol;> mGraphicOverlay&period;add&lpar;textGraphic&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<div> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Complete MainActivity Code<&sol;span><&sol;strong><&sol;h5> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;textrecognitionexp&semi;<br &sol;><br &sol;>import androidx&period;annotation&period;NonNull&semi;<br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;<br &sol;><br &sol;>import android&period;content&period;Context&semi;<br &sol;>import android&period;content&period;res&period;AssetManager&semi;<br &sol;>import android&period;graphics&period;Bitmap&semi;<br &sol;>import android&period;graphics&period;BitmapFactory&semi;<br &sol;>import android&period;os&period;Bundle&semi;<br &sol;>import android&period;view&period;View&semi;<br &sol;>import android&period;widget&period;Button&semi;<br &sol;>import android&period;widget&period;ImageView&semi;<br &sol;>import android&period;widget&period;Toast&semi;<br &sol;><br &sol;>import com&period;google&period;android&period;gms&period;tasks&period;OnFailureListener&semi;<br &sol;>import com&period;google&period;android&period;gms&period;tasks&period;OnSuccessListener&semi;<br &sol;>import com&period;google&period;mlkit&period;vision&period;common&period;InputImage&semi;<br &sol;>import com&period;google&period;mlkit&period;vision&period;text&period;Text&semi;<br &sol;>import com&period;google&period;mlkit&period;vision&period;text&period;TextRecognition&semi;<br &sol;>import com&period;google&period;mlkit&period;vision&period;text&period;TextRecognizer&semi;<br &sol;><br &sol;>import java&period;io&period;IOException&semi;<br &sol;>import java&period;io&period;InputStream&semi;<br &sol;>import java&period;util&period;List&semi;<br &sol;><br &sol;>public class MainActivity extends AppCompatActivity &lbrace;<br &sol;><br &sol;> private ImageView mImageView&semi;<br &sol;> private Button mFindTextBtn&semi;<br &sol;> private Bitmap mSelectedImage&semi;<br &sol;> private GraphicOverlay mGraphicOverlay&semi;<br &sol;><br &sol;> &commat;Override<br &sol;> protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;&semi;<br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;<br &sol;><br &sol;> mImageView &equals; findViewById&lpar;R&period;id&period;image&lowbar;view&rpar;&semi;<br &sol;><br &sol;> mFindTextBtn &equals; findViewById&lpar;R&period;id&period;btn&lowbar;find&lowbar;text&rpar;&semi;<br &sol;><br &sol;> mGraphicOverlay &equals; findViewById&lpar;R&period;id&period;graphic&lowbar;overlay&rpar;&semi;<br &sol;><br &sol;> mSelectedImage &equals; getBitmapFromAsset&lpar;this&comma; "page&period;png"&rpar;&semi;<br &sol;> mImageView&period;setImageBitmap&lpar;mSelectedImage&rpar;&semi;<br &sol;><br &sol;> mFindTextBtn&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onClick&lpar;View v&rpar; &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;if bitmap is not null<&sol;strong><&sol;span><br &sol;> if &lpar;mSelectedImage &excl;&equals; null&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Creates a new bitmap&comma; scaled from an existing bitmap<&sol;span><&sol;strong><br &sol;> Bitmap resizedBitmap &equals; createScaleFactorUsingBitmap&lpar;mSelectedImage&rpar;&semi;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;setting new scaled bitmap in imageview<&sol;span><&sol;strong><br &sol;> mImageView&period;setImageBitmap&lpar;resizedBitmap&rpar;&semi;<br &sol;> mSelectedImage &equals; resizedBitmap&semi;<br &sol;> &rcub;<br &sol;> runTextRecognition&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;&semi;<br &sol;><br &sol;> &rcub;<br &sol;><br &sol;> private Bitmap createScaleFactorUsingBitmap&lpar;Bitmap mSelectedImage&rpar; &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Determine how much to scale down the image<&sol;strong><&sol;span><br &sol;> float scaleFactor &equals;<br &sol;> Math&period;max&lpar;<br &sol;> &lpar;float&rpar; mSelectedImage&period;getWidth&lpar;&rpar; &sol; &lpar;float&rpar; mImageView&period;getWidth&lpar;&rpar;&comma;<br &sol;> &lpar;float&rpar; mSelectedImage&period;getHeight&lpar;&rpar; &sol; &lpar;float&rpar; mImageView&period;getHeight&lpar;&rpar;&rpar;&semi;<br &sol;><br &sol;> Bitmap resizedBitmap &equals;<br &sol;> Bitmap&period;createScaledBitmap&lpar;<br &sol;> mSelectedImage&comma;<br &sol;> &lpar;int&rpar; &lpar;mSelectedImage&period;getWidth&lpar;&rpar; &sol; scaleFactor&rpar;&comma;<br &sol;> &lpar;int&rpar; &lpar;mSelectedImage&period;getHeight&lpar;&rpar; &sol; scaleFactor&rpar;&comma;<br &sol;> true&rpar;&semi;<br &sol;><br &sol;> return resizedBitmap&semi;<br &sol;> &rcub;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;recognize and extract text from image bitmap<&sol;strong><&sol;span><br &sol;> private void runTextRecognition&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;prepare input image using bitmap<&sol;span><&sol;strong><br &sol;> InputImage image &equals; InputImage&period;fromBitmap&lpar;mSelectedImage&comma; 0&rpar;&semi;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;creating TextRecognizer instance<&sol;strong><&sol;span><br &sol;> TextRecognizer recognizer &equals; TextRecognition&period;getClient&lpar;&rpar;&semi;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;process the image<&sol;span><&sol;strong><br &sol;> recognizer&period;process&lpar;image&rpar;<br &sol;> &period;addOnSuccessListener&lpar;<br &sol;> new OnSuccessListener&lt&semi;Text&gt&semi;&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onSuccess&lpar;Text texts&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Task completed successfully&period;<&sol;span><&sol;strong><br &sol;> processTextRecognitionResult&lpar;texts&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> &period;addOnFailureListener&lpar;<br &sol;> new OnFailureListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onFailure&lpar;&commat;NonNull Exception e&rpar; &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Task failed with an exception<&sol;strong><&sol;span><br &sol;> e&period;printStackTrace&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;perform operation on the full text recognized in the image&period;<&sol;strong><&sol;span><br &sol;> private void processTextRecognitionResult&lpar;Text texts&rpar; &lbrace;<br &sol;><br &sol;> List&lt&semi;Text&period;TextBlock&gt&semi; blocks &equals; texts&period;getTextBlocks&lpar;&rpar;&semi;<br &sol;> if &lpar;blocks&period;size&lpar;&rpar; &equals;&equals; 0&rpar; &lbrace;<br &sol;> Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "No text found"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;<br &sol;> return&semi;<br &sol;> &rcub;<br &sol;> mGraphicOverlay&period;clear&lpar;&rpar;&semi;<br &sol;> for &lpar;Text&period;TextBlock block &colon; texts&period;getTextBlocks&lpar;&rpar;&rpar; &lbrace;<br &sol;> for &lpar;Text&period;Line line &colon; block&period;getLines&lpar;&rpar;&rpar; &lbrace;<br &sol;> for &lpar;Text&period;Element element &colon; line&period;getElements&lpar;&rpar;&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Draws the bounding box around the element&period;<&sol;span><&sol;strong><br &sol;> GraphicOverlay&period;Graphic textGraphic &equals; new TextGraphic&lpar;mGraphicOverlay&comma; element&rpar;&semi;<br &sol;> mGraphicOverlay&period;add&lpar;textGraphic&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;get bitmap of image from app assets&period;<&sol;strong><&sol;span><br &sol;> public Bitmap getBitmapFromAsset&lpar;Context context&comma; String filePath&rpar; &lbrace;<br &sol;> AssetManager assetManager &equals; context&period;getAssets&lpar;&rpar;&semi;<br &sol;><br &sol;> InputStream is&semi;<br &sol;> Bitmap bitmap &equals; null&semi;<br &sol;> try &lbrace;<br &sol;> is &equals; assetManager&period;open&lpar;filePath&rpar;&semi;<br &sol;> bitmap &equals; BitmapFactory&period;decodeStream&lpar;is&rpar;&semi;<br &sol;> &rcub; catch &lpar;IOException e&rpar; &lbrace;<br &sol;> e&period;printStackTrace&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;> return bitmap&semi;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<p>When you run the app it will look like this as  shown below&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-2984" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;12&sol;Screenshot&lowbar;20211204-205741&lowbar;TextRecognitionExp-498x1024&period;jpg" alt&equals;"" width&equals;"261" height&equals;"537" &sol;>        <img class&equals;"alignnone wp-image-2985" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;12&sol;Screenshot&lowbar;20211204-205752&lowbar;TextRecognitionExp-498x1024&period;jpg" alt&equals;"" width&equals;"261" height&equals;"537" &sol;>&NewLine;

Exit mobile version