<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nuwan Dissanayaka</title>
    <description>The latest articles on DEV Community by Nuwan Dissanayaka (@nuwan_dissanayaka).</description>
    <link>https://dev.to/nuwan_dissanayaka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4274937%2Fbdb5d5a3-e833-435e-b154-9ad846e19299.jpg</url>
      <title>DEV Community: Nuwan Dissanayaka</title>
      <link>https://dev.to/nuwan_dissanayaka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nuwan_dissanayaka"/>
    <language>en</language>
    <item>
      <title>Clean up a scanned page on Android: deskew, threshold, and kill the shadow</title>
      <dc:creator>Nuwan Dissanayaka</dc:creator>
      <pubDate>Mon, 30 Nov 2026 20:46:23 +0000</pubDate>
      <link>https://dev.to/nuwan_dissanayaka/clean-up-a-scanned-page-on-android-deskew-threshold-and-kill-the-shadow-2mkd</link>
      <guid>https://dev.to/nuwan_dissanayaka/clean-up-a-scanned-page-on-android-deskew-threshold-and-kill-the-shadow-2mkd</guid>
      <description>&lt;p&gt;Between finding the edges of a page and reading the text off it, there is a step that decides whether your scanner looks professional or looks like a photo someone took of a desk. It is the cleanup: taking the cropped, perspective-corrected image and turning it into something that reads like a document scanned on a flat-bed, white where the paper is white and black where the ink is.&lt;/p&gt;

&lt;p&gt;Skip it and two things downstream suffer. OCR accuracy drops, because the model is fighting shadows and grey paper. And the exported PDF looks amateurish next to anything from a real scanner. I spent more time on this stage than I expected to, mostly because the steps have to happen in the right order, and the tutorials rarely say what that order is.&lt;/p&gt;

&lt;p&gt;Here is the pipeline that works, using OpenCV on Android, and why each step sits where it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grayscale first, but do not threshold yet
&lt;/h2&gt;

&lt;p&gt;Convert to a single channel to work with. For a document, color is mostly noise at this stage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cvtColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;COLOR_BGR2GRAY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tempting next move is to threshold straight to black and white. Resist it, because the lighting is still uneven and thresholding an unevenly lit page is where most home-grown scanners fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kill the shadow before anything else
&lt;/h2&gt;

&lt;p&gt;A phone held over a page casts a shadow, and the light falls off toward the edges. If you threshold now, the darker corners turn into black smears while the bright center turns clean, and no single cutoff works for the whole page.&lt;/p&gt;

&lt;p&gt;The fix is to estimate the lighting and divide it out. Blow the text away with a large dilation followed by a median blur, which leaves you an image of just the background and its shadow. Divide the original by that, and you flatten the illumination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;kernel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStructuringElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MORPH_RECT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;7.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;7.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dilate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kernel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;medianBlur&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// gray / background, scaled back to 0-255, removes the shadow gradient&lt;/span&gt;
&lt;span class="nc"&gt;Core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;background&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;255.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one step does more for a scan than any other, and it is the one most pipelines skip. Run it before you threshold or boost contrast, because every later step assumes even lighting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then decide: black and white, or grayscale
&lt;/h2&gt;

&lt;p&gt;Give the user both, because they are scanning different things. A page of text should go to clean black and white. A photo, an ID with a face, or a colored diagram should stay as a toned grayscale or color image, because thresholding destroys it.&lt;/p&gt;

&lt;p&gt;For the text case, use an adaptive threshold rather than a global one, so the decision is made locally and survives any lighting you did not fully remove.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;adaptiveThreshold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blackWhite&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;255.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ADAPTIVE_THRESH_GAUSSIAN_C&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;THRESH_BINARY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;// block size, constant subtracted&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block size matters and has to scale with resolution. A value tuned on a downscaled preview will be wrong on the full-resolution capture, producing either speckled noise or hollow letters, so compute it from the image size instead of hardcoding 15.&lt;/p&gt;

&lt;p&gt;For the grayscale case, a gentle contrast lift with CLAHE is usually enough, and it avoids the harsh look of a global contrast stretch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;clahe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createCLAHE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;8.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;8.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;clahe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enhanced&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go easy here. Over-sharpening and aggressive contrast feel like they should help OCR, and they do the opposite, because they turn paper texture and compression noise into fake edges the recognizer then tries to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deskew the last few degrees
&lt;/h2&gt;

&lt;p&gt;Edge detection and the perspective warp get the page mostly straight, but a page is often still rotated a degree or two, and crooked text looks careless and reads worse. Estimate the skew from the thresholded image, where the text rows are the strongest horizontal structure.&lt;/p&gt;

&lt;p&gt;Collect the foreground pixels, fit a minimum-area rectangle around them, and read its angle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;coords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findNonZero&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blackWhiteInverted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coords&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;minAreaRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MatOfPoint2f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;coords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;angle&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt; &lt;span class="c1"&gt;// normalize the quadrant&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRotationMatrix2D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Imgproc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warpAffine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;deskewed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;if (angle &amp;lt; -45)&lt;/code&gt; line is not optional. &lt;code&gt;minAreaRect&lt;/code&gt; reports its angle in a range that flips near the diagonal, and without the correction a page tilted one way occasionally rotates ninety degrees the other way. It is a rare, confusing bug that only shows up on certain inputs, which is exactly why it survives into shipped apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do it once, at capture, not per frame
&lt;/h2&gt;

&lt;p&gt;Every step here is too heavy to run on the live camera preview. The preview only needs the cheap edge detection that draws the overlay. Save the full cleanup, at full resolution, for the single frame you capture, and run it off the main thread so the UI does not freeze while it works. Trying to do this per frame is how you turn a phone into a hand warmer.&lt;/p&gt;

&lt;p&gt;That order is the point: grayscale, remove the shadow, then threshold or enhance, then deskew. Get the shadow out before you threshold and most of the "my scans look grey and dirty" problem disappears on its own.&lt;/p&gt;

&lt;p&gt;Disclosure: I build &lt;a href="https://paperora.app" rel="noopener noreferrer"&gt;Paperora&lt;/a&gt;, an Android document scanner, and this is the cleanup stage it runs on every page. It all happens on the device, so the image is processed on the phone and nothing is uploaded. If you are building a scanner, treat this stage as a real feature rather than a filter you bolt on at the end. The straight, evenly lit, cleanly thresholded page is what makes the OCR accurate and the PDF look like it came from a real scanner instead of a camera.&lt;/p&gt;

</description>
      <category>android</category>
      <category>opencv</category>
      <category>kotlin</category>
      <category>computervision</category>
    </item>
    <item>
      <title>Make a PDF from scanned pages on Android, no server and no library</title>
      <dc:creator>Nuwan Dissanayaka</dc:creator>
      <pubDate>Mon, 30 Nov 2026 20:40:54 +0000</pubDate>
      <link>https://dev.to/nuwan_dissanayaka/make-a-pdf-from-scanned-pages-on-android-no-server-and-no-library-4cf5</link>
      <guid>https://dev.to/nuwan_dissanayaka/make-a-pdf-from-scanned-pages-on-android-no-server-and-no-library-4cf5</guid>
      <description>&lt;p&gt;Most scanner apps reach for a third-party PDF library or a server round trip to turn page images into a document. Android has shipped a built-in API for this since API 19, &lt;code&gt;android.graphics.pdf.PdfDocument&lt;/code&gt;, and for scanned pages it is usually all you need. No dependency, and nothing leaves the phone. The catch is that the obvious way to use it produces enormous files, and nobody tells you that until a user emails you a 40 MB scan of a three-page letter.&lt;/p&gt;

&lt;p&gt;I ship this in a scanner app, so here is the version that works, and the two places it bites.&lt;/p&gt;

&lt;h2&gt;
  
  
  The minimal version
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;PdfDocument&lt;/code&gt; is a set of pages, and each page is a &lt;code&gt;Canvas&lt;/code&gt; you draw onto. For a scan, the thing you draw is the cleaned, cropped bitmap of the page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forEachIndexed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;pageInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PageInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pageInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finishPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;FileOutputStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outFile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That runs, and it opens in any PDF viewer. It is also wrong in two ways that do not show up until later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gotcha one: page size is in points, not pixels
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;PageInfo.Builder&lt;/code&gt; takes a width and height in points, where a point is 1/72 of an inch. The code above passes the bitmap's pixel dimensions, so a 2000-pixel-wide scan becomes a page roughly 27 inches wide. It looks fine on a phone screen because the viewer scales it to fit, then someone prints it and gets a document the size of a poster.&lt;/p&gt;

&lt;p&gt;Size the page to real paper and draw the bitmap into it, preserving the aspect ratio so the scan does not stretch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A4 at 72 points per inch is 595 x 842 points.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;pageInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PageInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;595&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;842&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pageInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;dest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fitInside&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;595&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;842&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finishPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;fitInside&lt;/code&gt; is the usual letterbox math: scale the image to the largest size that fits the page, then center it. If your users scan US Letter rather than A4, that is 612 x 792 points, and in practice you want to detect the page's own aspect ratio and pick the closer standard rather than forcing everything to one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gotcha two: the files are huge
&lt;/h2&gt;

&lt;p&gt;This is the one that surprises people. &lt;code&gt;PdfDocument&lt;/code&gt; stores the page content with lossless (Flate) compression. That is fine for vector graphics and text, but a photo of a page does not compress losslessly in any meaningful way, so a full-resolution camera bitmap lands in the PDF at close to its raw size. Three pages at 12 megapixels each can produce a document tens of megabytes large, which is miserable to email and slow to open.&lt;/p&gt;

&lt;p&gt;There is no quality knob on &lt;code&gt;PdfDocument&lt;/code&gt; to fix this, so you fix it before you draw. The lever is the bitmap itself: fewer pixels and a pre-compression pass through JPEG.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Downscale so the long edge is about 2000px. That is plenty for a readable&lt;/span&gt;
&lt;span class="c1"&gt;// scan and cuts the pixel count dramatically.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;downscaleLongEdge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxEdge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Round-trip through JPEG so what you draw is already compressed data.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;jpegBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ByteArrayOutputStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;scaled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CompressFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JPEG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;compressed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BitmapFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decodeByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jpegBytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jpegBytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;compressed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A long edge around 2000 pixels keeps text sharp enough to read and to run OCR against, while taking a page from several megabytes down to a few hundred kilobytes. Expose the quality as a setting if your users care, because someone archiving a contract wants more detail than someone texting a receipt. The JPEG round trip is what most "how to make a PDF on Android" tutorials skip, and it is the whole difference between a shareable file and one that bounces off a mail server.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you want the text to be selectable
&lt;/h2&gt;

&lt;p&gt;A PDF built this way is an image. You cannot select or search the text in it, because there is no text in it, only a picture of text. To make it searchable you overlay the recognized words as an invisible text layer, drawing each OCR result at its bounding box with a fully transparent paint so the visible page still shows the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;transparent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Paint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TRANSPARENT&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ocrResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mapToPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;boundingBox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// image coords to page coords&lt;/span&gt;
    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;textSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting the text to line up with the image under it is fiddly, and it only works as well as your OCR does. I wrote up the on-device OCR side of this separately, including why the bounding boxes matter. The short version: do the recognition first, keep the boxes, and treat the text layer as a second pass over the same page you already drew.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where PdfDocument runs out
&lt;/h2&gt;

&lt;p&gt;It has no password protection or encryption, and no real control over compression beyond what you do to the bitmaps yourself. It only builds new PDFs, so it will not merge existing ones. And there is no page-level incremental save, so a hundred-page scan holds a hundred pages of bitmaps in memory while you write. If you need any of that, a library like PDFBox for Android or a commercial SDK earns its place.&lt;/p&gt;

&lt;p&gt;For the common job, though, which is turning a handful of scanned pages into a clean, reasonably sized, shareable document, the built-in API does it with no dependency and nothing leaving the phone.&lt;/p&gt;

&lt;p&gt;Disclosure: I build &lt;a href="https://paperora.app" rel="noopener noreferrer"&gt;Paperora&lt;/a&gt;, an Android document scanner, and this is the export path it uses. Scanning, cleanup, OCR and PDF creation all run on the device, so the file is built on the phone and never uploaded. If you are adding PDF export to an app, start with &lt;code&gt;PdfDocument&lt;/code&gt;, measure the output size on a real multi-page scan early, and only reach for a library once you hit something it genuinely cannot do. Most of the time you never will.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>pdf</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Document edge detection on Android: how a scanner finds the page</title>
      <dc:creator>Nuwan Dissanayaka</dc:creator>
      <pubDate>Mon, 30 Nov 2026 20:38:56 +0000</pubDate>
      <link>https://dev.to/nuwan_dissanayaka/document-edge-detection-on-android-how-a-scanner-finds-the-page-52hb</link>
      <guid>https://dev.to/nuwan_dissanayaka/document-edge-detection-on-android-how-a-scanner-finds-the-page-52hb</guid>
      <description>&lt;p&gt;The feature people notice first in a scanner app is not the OCR or the PDF export. It is the blue box that snaps to the edges of the page the moment you point the camera at it. When it works it feels like magic. When it misses, the whole app feels broken, because every scan after that starts from a crooked, half-cropped image.&lt;/p&gt;

&lt;p&gt;I work on a document scanner, so I have spent more time on this one feature than I expected to. Here is how edge detection works on Android, the two realistic ways to build it, and where it breaks once real users point real cameras at real paper.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three steps behind the blue box
&lt;/h2&gt;

&lt;p&gt;The job here is specific: find the four corners of a document sitting at an angle inside a larger, messy photo, then warp that quadrilateral back into a flat, straight rectangle. Three steps hide inside the blue box:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the document boundary in the camera frame.&lt;/li&gt;
&lt;li&gt;Reduce that boundary to four corners.&lt;/li&gt;
&lt;li&gt;Apply a perspective transform so the output looks like the page was scanned flat.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step three is the one that makes a photo of a receipt on a desk look like a real scan. Skip it and you get a trapezoid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 1: ML Kit Document Scanner
&lt;/h2&gt;

&lt;p&gt;If you are starting today, try this first. Google ships an &lt;a href="https://developers.google.com/ml-kit/vision/doc-scanner" rel="noopener noreferrer"&gt;ML Kit Document Scanner&lt;/a&gt; that gives you the entire capture flow: the live edge overlay, auto-capture, the corner handles for manual correction, and the perspective-corrected result. You launch it as an activity and get back cropped page images or a PDF.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GmsDocumentScannerOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setGalleryImportAllowed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPageLimit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setResultFormats&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;RESULT_FORMAT_JPEG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;RESULT_FORMAT_PDF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setScannerMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SCANNER_MODE_FULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nc"&gt;GmsDocumentScanning&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStartScanIntent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addOnSuccessListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;intentSender&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;scannerLauncher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IntentSenderRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intentSender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model downloads through Google Play Services the first time, so your APK stays small, and the detection quality is better than most teams will build themselves in a reasonable amount of time.&lt;/p&gt;

&lt;p&gt;The catch is control. You get Google's capture UI, Google's auto-crop behaviour, and Google's corner-adjust screen. If your app needs its own camera screen, its own branding on that screen, custom filters applied during capture, or batch scanning that does not match their flow, you hit a wall. It is the right call when the scan is a feature inside a bigger app, and the wrong call when scanning is the app and the capture experience is your product.&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 2: an OpenCV contour pipeline
&lt;/h2&gt;

&lt;p&gt;The classic do-it-yourself approach runs on every frame from the camera and looks roughly like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Downscale the frame. You do not need full resolution to find a page, and the smaller image keeps you near real time.&lt;/li&gt;
&lt;li&gt;Grayscale, then blur to kill noise (a Gaussian blur works).&lt;/li&gt;
&lt;li&gt;Run edge detection (Canny) to get a binary edge map.&lt;/li&gt;
&lt;li&gt;Find contours, keep the large ones, and approximate each to a polygon.&lt;/li&gt;
&lt;li&gt;Pick the largest four-sided convex polygon whose area is a sensible fraction of the frame. That is your page.&lt;/li&gt;
&lt;li&gt;Map the four corners back to full-resolution coordinates and apply a perspective warp.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The reason this stays popular is that you own every step. You can tune how aggressive the detection is, draw your own overlay, decide when to auto-capture, and apply your own image cleanup to the warped result. The cost is that you now own every failure too, and there are more of them than the happy-path tutorials admit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it fails on real paper
&lt;/h2&gt;

&lt;p&gt;A white page on a white table is the hardest case. Contour detection needs contrast between the document and the surface behind it, and a white sheet on a light desk has almost none, so the edges you find belong to shadows rather than the page. There is no clean fix. You raise contrast in preprocessing, you lean on the faint shadow the page casts, and you keep manual corner handles so the person can rescue a scan the detector gave up on. Do not ship without that manual fallback.&lt;/p&gt;

&lt;p&gt;The four-corner assumption breaks more often than you would think. Real documents get held in a hand that covers a corner, folded so the boundary is not convex, or photographed with the top edge running off the frame. A strict largest-convex-quadrilateral rule rejects all of these, so you have to decide what to do when you cannot find four clean corners: fall back to the frame edges, keep the last good detection for a few frames, or let the person drag the corners themselves.&lt;/p&gt;

&lt;p&gt;Per-frame detection drains the battery and the frame rate. Running the full pipeline on every camera frame at full resolution heats the phone and drops you below a usable preview. Downscale hard for detection, run it off the main thread, and do the expensive full-resolution warp only once, at capture.&lt;/p&gt;

&lt;p&gt;Auto-capture is a UX problem wearing a computer-vision costume. Firing the shutter the instant you find four corners feels jumpy and grabs blurry frames mid-movement. What works is waiting until the detected quad has been stable for a short window and the frame is sharp enough, then capturing. Getting that timing right took me more iterations than the detection math did.&lt;/p&gt;

&lt;p&gt;The warp needs the corners in a known order. Perspective transforms care which corner is top-left, so sort your four points into a consistent order (top-left, top-right, bottom-right, bottom-left) before you build the transform, or the output comes out mirrored or rotated and you will never work out why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which one to pick
&lt;/h2&gt;

&lt;p&gt;If you want a scan feature and not a scanning product, use the ML Kit Document Scanner and spend your time elsewhere. You get strong detection and a maintained UI for almost no code.&lt;/p&gt;

&lt;p&gt;If the capture screen is your product, or you need control the managed flow does not give you, build the OpenCV pipeline. Budget for the failures above, treat manual corner adjustment as a real feature rather than an afterthought, and test on bad lighting and busy backgrounds early, because that is where real scans happen.&lt;/p&gt;

&lt;p&gt;Disclosure: I build &lt;a href="https://paperora.app" rel="noopener noreferrer"&gt;Paperora&lt;/a&gt;, an Android document scanner, so this is the problem I stare at. We run edge detection, the perspective warp, OCR and PDF creation on the device, so scans stay on the phone and the app works offline. If you are building in this space, get the detection and the manual fallback right first. The OCR and the clean PDF downstream are only ever as good as the straight, well-cropped page you feed them.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>opencv</category>
      <category>computervision</category>
    </item>
    <item>
      <title>On-device OCR on Android with ML Kit: read text from a scan without a server</title>
      <dc:creator>Nuwan Dissanayaka</dc:creator>
      <pubDate>Mon, 30 Nov 2026 18:50:21 +0000</pubDate>
      <link>https://dev.to/nuwan_dissanayaka/on-device-ocr-on-android-with-ml-kit-read-text-from-a-scan-without-a-server-48ol</link>
      <guid>https://dev.to/nuwan_dissanayaka/on-device-ocr-on-android-with-ml-kit-read-text-from-a-scan-without-a-server-48ol</guid>
      <description>&lt;p&gt;Most document scanner apps send your page to a server to pull the text out of it. For a receipt or a signed contract, that means a photo of something you probably did not want to upload is now sitting in someone else's logs. You do not have to build it that way. Android can read the text off an image on the device, with no network call at all, and the API for it is free.&lt;/p&gt;

&lt;p&gt;I have shipped this in a scanner app that runs OCR on the phone, and the quickstart gets you a demo in ten minutes but hides the parts that actually matter in production. Here is the whole thing, including the parts that bit me.&lt;/p&gt;

&lt;h2&gt;
  
  
  What ML Kit gives you
&lt;/h2&gt;

&lt;p&gt;ML Kit's Text Recognition v2 runs a model locally and returns the text in an image. On-device means the bytes never leave the phone, it works in airplane mode, and there is no per-call cost because there is no call. Latency is a few hundred milliseconds on a mid-range phone instead of a round trip plus server time.&lt;/p&gt;

&lt;p&gt;The Latin-script model is the default. Chinese, Devanagari, Japanese and Korean each have their own model you pull in separately, so you are not shipping scripts you do not need.&lt;/p&gt;

&lt;p&gt;You get two ways to ship the model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bundled&lt;/strong&gt; (&lt;code&gt;com.google.mlkit:text-recognition&lt;/code&gt;): the model ships inside your APK. It works on first launch with no download, and it adds a few MB to your app size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unbundled&lt;/strong&gt; (&lt;code&gt;com.google.android.gms:play-services-mlkit-text-recognition&lt;/code&gt;): Google Play Services downloads the model the first time it is needed. Smaller APK, but the first OCR call can fail or stall if the download has not finished.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// build.gradle (bundled Latin model)&lt;/span&gt;
&lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.google.mlkit:text-recognition:16.0.1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The minimal working version
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;recognizer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TextRecognition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TextRecognizerOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DEFAULT_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// rotationDegrees matters. See the gotchas below.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;InputImage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rotationDegrees&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;recognizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addOnSuccessListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textBlocks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// line.text is the string, line.boundingBox is where it sits&lt;/span&gt;
                &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;d&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"OCR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${line.text} @ ${line.boundingBox}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addOnFailureListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;e&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"OCR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"recognition failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is a tree: blocks hold lines, lines hold elements (roughly words), and every node carries a &lt;code&gt;boundingBox&lt;/code&gt; and the recognized text. Most apps want &lt;code&gt;result.text&lt;/code&gt; for the raw dump, but the boxes are what let you rebuild layout, highlight a field, or line up a translation over the original.&lt;/p&gt;

&lt;h2&gt;
  
  
  The parts the quickstart skips
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Clean the image before you hand it over.&lt;/strong&gt; OCR accuracy on a raw camera frame is noticeably worse than on a cropped, deskewed, contrast-corrected page. The order that worked for me is: detect the document edges, warp to a flat rectangle, convert to grayscale, then push that into ML Kit. Feeding the model a full photo of a page lying at an angle on a desk is the single biggest cause of garbage output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rotation is a real bug source.&lt;/strong&gt; &lt;code&gt;InputImage&lt;/code&gt; needs the correct &lt;code&gt;rotationDegrees&lt;/code&gt;, and camera frames are almost never upright. If you pass a &lt;code&gt;media.Image&lt;/code&gt; from CameraX, read the rotation from the &lt;code&gt;ImageProxy&lt;/code&gt;. If you forget this, the model quietly returns worse results instead of failing, so it is easy to miss until someone scans a landscape receipt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do not run it on the main thread for batch work.&lt;/strong&gt; One frame is fine. Running OCR across twenty pages of a scan on the UI thread will jank the app. Move batch recognition to a background dispatcher and show progress.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Close the recognizer.&lt;/strong&gt; &lt;code&gt;TextRecognizer&lt;/code&gt; holds native resources. Call &lt;code&gt;recognizer.close()&lt;/code&gt; when you are done, or tie it to the lifecycle, or you will leak.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decide bundled vs unbundled on purpose.&lt;/strong&gt; If OCR is a headline feature people use on first launch, bundle the model so it is there offline from minute one. If it is a secondary feature and app size matters, go unbundled, but then handle the "model still downloading" state with a real message instead of a silent failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it still falls short
&lt;/h2&gt;

&lt;p&gt;It reads printed text well. It does not read handwriting in any way you can trust. Low light and motion blur drop accuracy fast, which is another reason the edge-detect-and-clean step earns its place. Tables come back as a stream of lines, so if you need the grid you have to reconstruct columns yourself from the bounding boxes, and that is its own project. And the bundled model is Latin only, so anything beyond that is an extra model and an extra decision about app size.&lt;/p&gt;

&lt;p&gt;None of that makes it the wrong choice. It makes it a tool with edges, and knowing the edges is the difference between a demo and something people rely on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I went on-device
&lt;/h2&gt;

&lt;p&gt;Disclosure: I build &lt;a href="https://paperora.app" rel="noopener noreferrer"&gt;Paperora&lt;/a&gt;, an Android document scanner, so I had a reason to care about this beyond a blog post. The whole app runs scanning, PDF creation and OCR on the phone, so documents stay on the device and it works with no signal. That was a product decision as much as a privacy one: people scan IDs, medical forms and contracts, and "we never had your file in the first place" is a stronger promise than "we deleted it."&lt;/p&gt;

&lt;p&gt;If you are building something in this space, start on-device and only reach for a server when you hit something the on-device model genuinely cannot do. You will ship a faster, cheaper, more private app, and most of the time you will never need the server at all.&lt;/p&gt;

&lt;p&gt;Happy to answer ML Kit questions in the comments.&lt;/p&gt;

</description>
      <category>android</category>
      <category>machinelearning</category>
      <category>kotlin</category>
      <category>privacy</category>
    </item>
  </channel>
</rss>
