<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tushar Gusain on Medium]]></title>
        <description><![CDATA[Stories by Tushar Gusain on Medium]]></description>
        <link>https://medium.com/@tushargusain40?source=rss-246d3cfcb899------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*m7aYsXVMsxPXsCrc.jpg</url>
            <title>Stories by Tushar Gusain on Medium</title>
            <link>https://medium.com/@tushargusain40?source=rss-246d3cfcb899------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:06:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tushargusain40/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using OpenCV in Xcode to paint the wall in an image]]></title>
            <link>https://tushargusain40.medium.com/using-opencv-in-xcode-to-paint-the-wall-in-an-image-c0ceffed949b?source=rss-246d3cfcb899------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0ceffed949b</guid>
            <category><![CDATA[paint]]></category>
            <category><![CDATA[uistoryboard]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Tushar Gusain]]></dc:creator>
            <pubDate>Sun, 11 Oct 2020 11:36:13 GMT</pubDate>
            <atom:updated>2021-02-13T08:25:43.359Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn about iOS Camera, OpenCV, and storing images in this Xcode Tutorial.</h4><h3>Version:-</h3><p>Xcode 11, swift 5, iOS 13</p><p>This is a post by Tushar Gusain, an Android and iOS developer.</p><p>A good paint job beautifies and adds character and personality to your home. Just for that reason it’s important to take sufficient time planning which colors, shades, and paint styles you want to decorate your home with.</p><p>Whenever you buy a new house, whitewash your home, or just want to repaint it you are stuck at deciding which color will suit best for the walls. Making an app that’ll help you visualize those walls in different colours will come in handy.</p><p>We can do this by taking a photo of one or more walls with your phone and clicking the wall to paint them in the chosen colour or texture. Painting the wall, i.e. image processing part can be done with the help of OpenCV library.</p><p>Keep reading to know how simple it is to integrate OpenCV in your app.</p><h3>About OpenCV</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8j_Cc2XfgxfYLtbS" /></figure><p><strong>OpenCV</strong> (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel).</p><p>It is an open source library with quite a number of useful Algorithms and API which can be used for image processing. It is mainly used to do all the operation related to Images, like:</p><p><strong>1. </strong>Reading and Writing Images.</p><p>2. Detecting faces and its features.</p><p>3. Detecting shapes in an image.</p><p>4. Text recognition.</p><p>5. Modifying image quality and colors.</p><p>6. Developing Augmented reality apps.</p><p>Etc…..</p><p>OpenCV supports a wide variety of programming languages such as C++, Python, Java, etc., and is available on different platforms including Windows, Linux, OS X, Android, and iOS. Interfaces for high-speed GPU operations based on CUDA and OpenCL are also under active development.</p><p>( To know more about opencv follow this link: <a href="https://opencv.org/about/">https://opencv.org/about/</a> )</p><h3>Creating your Xcode project</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F5zf2M4HgjjWszLd4a5%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F5zf2M4HgjjWszLd4a5%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExODhsMmNqYnZpYnNocjd3MTFkd2k2Mm96dzEwbTg2OGdwcGplMzQ1bSZlcD12MV9naWZzX2dpZklkJmN0PWc%2F5zf2M4HgjjWszLd4a5%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="181" frameborder="0" scrolling="no"><a href="https://medium.com/media/ceb4fd2380af661521a6e88c61bea3a3/href">https://medium.com/media/ceb4fd2380af661521a6e88c61bea3a3/href</a></iframe><p>Open <strong>Xcode</strong>, click on <strong>New project</strong>, then select <strong>Single View App</strong> and click <strong>Next</strong>.</p><p>Now, Enter the name of your App in the Product<strong> Name</strong> field, then, select <strong>Swift </strong>as Language and <strong>Storyboard </strong>as User Interface.</p><p>After that click on <strong>Next</strong>.</p><p>Open <strong>Xcode</strong>, click on <strong>New project</strong>, then select <strong>Single View App</strong> and click <strong>Next</strong>.</p><p>Now, Enter the name of your App in the Product<strong> Name</strong> field, then, select <strong>Swift </strong>as Language and <strong>Storyboard </strong>as User Interface.</p><p>After that click on <strong>Next</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/0*cieb5iWeDQTh-tWA" /></figure><h3>Integrating the OpenCV module</h3><h3>Step 1: Downloading OpenCV</h3><p>Download the OpenCV library from here: <a href="https://opencv.org/releases/">https://opencv.org/releases/<br></a>For this project I am using OpenCV — 3.4.7, after downloading unzip the folder.</p><h3>Step 2: Importing OpenCV</h3><p>Once downloaded let’s import it into our Paint App Storyboard app target.</p><p>Drag and drop opencv2.framework into the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ou3aQvkYadtU_TtT" /></figure><p>Our selection of the adding <strong>opencv2.framework</strong> to the project options will copy <strong>opencv2.framework</strong> into our project and link the framework to our app.</p><p>You should find <strong>opencv2.framework</strong> in <strong>Frameworks, Libraries and Embedded Content</strong> under <strong>General</strong> tab for the <strong>Paint App Storyboard</strong> target configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bXJAmE2wC8dV0VIR" /></figure><h3>Building the User Interface</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl0HlIgRZncAcDJw40%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl0HlIgRZncAcDJw40%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl0HlIgRZncAcDJw40%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="240" frameborder="0" scrolling="no"><a href="https://medium.com/media/825a755994ad19b2f950789ed5d2fd9c/href">https://medium.com/media/825a755994ad19b2f950789ed5d2fd9c/href</a></iframe><h3>Step 1: Add a Toolbar.</h3><p>Go ahead and add a <strong>Toolbar</strong> to the bottom of your <strong>ViewController.</strong></p><p>After that add the following bar button items as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*noPEmpKezrDgQhed" /></figure><h3>Step 2: Add an ImageView.</h3><p>Now, add an <strong>ImageView</strong> to your <strong>Viewcontroller</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UtvFTnjUO_AcoFXq" /></figure><h3>Step 4: Hookup your User Interface.</h3><p>Change the name of your ViewController to <strong>PaintViewController.</strong></p><h4>a) Hookup your Outlets (ImageView).</h4><p>Add the below IBOutlet to the PaintViewController and hook it with the imageView.</p><pre>//MARK:<strong>- ViewController Outlets</strong></pre><pre><strong>@IBOutlet</strong> <strong>var</strong> imageView: UIImageView!</pre><h4>b) Add Property Variables</h4><p>Add the below local variables to the PaintViewController.</p><pre>//MARK:<strong>- ViewController state variables</strong></pre><pre><strong>private</strong> <strong>var</strong> touchX = 0.0</pre><pre><strong>private</strong> <strong>var</strong> touchY = 0.0</pre><pre><strong>private</strong> <strong>var</strong> screenSize = CGSize.zero</pre><pre><strong>private</strong> <strong>var</strong> cameraToggled = <strong>false</strong></pre><pre><strong>private</strong> <strong>var</strong> applyPaint = <strong>true</strong></pre><pre><strong>private</strong> <strong>var</strong> currentColor = UIColor.red</pre><pre><strong>private</strong> <strong>var</strong> imagePickerVC: UIImagePickerController {</pre><pre><strong>let</strong> vc = UIImagePickerController()</pre><pre>vc.delegate = <strong>self</strong></pre><pre>vc.allowsEditing = <strong>true</strong></pre><pre><strong>return</strong> vc</pre><pre>}</pre><h4>c) Hookup your Action Methods (Toolbar Buttons).</h4><p>Add the below lines of code to the PaintViewController and hook them with their respective bar buttons.</p><pre>//MARK:<strong>- ViewControllers Action methods</strong></pre><pre><strong>@IBAction</strong> <strong>func</strong> takePhoto(<strong>_</strong> sender: UIBarButtonItem) {</pre><pre>  cameraToggled = <strong>true</strong></pre><pre>  getPhoto()</pre><pre>}</pre><pre><strong>@IBAction</strong> <strong>func</strong> openGallery(<strong>_</strong> sender: UIBarButtonItem) {</pre><pre>  cameraToggled = <strong>false</strong></pre><pre>  getPhoto()</pre><pre>}</pre><pre><strong>@IBAction</strong> <strong>func</strong> toggleTexture(<strong>_</strong> sender: UIBarButtonItem) {</pre><pre>  applyPaint = <strong>false</strong></pre><pre>}</pre><pre><strong>@IBAction</strong> <strong>func</strong> toggleColor(<strong>_</strong> sender: UIBarButtonItem) {</pre><pre>  applyPaint = <strong>true</strong></pre><pre>}</pre><pre><strong>@IBAction</strong> <strong>func</strong> chooseColor(<strong>_</strong> sender: UIBarButtonItem) {</pre><pre>  colorPicker.isHidden = <strong>false</strong></pre><pre>}</pre><h3>Insert Wall Painter Algorithm</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FnvpzLVp88bF6%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FnvpzLVp88bF6%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FnvpzLVp88bF6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/9406a9e5831f668093f6729836701444/href">https://medium.com/media/9406a9e5831f668093f6729836701444/href</a></iframe><h3>Step 1: Add C++ header and implementation files</h3><p>From menu click on <strong>File &gt; New &gt; File…</strong> Next search and select for<strong> C++</strong> File template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/0*byGUN4oUlBK_2UCd" /></figure><p>Click <strong>Next</strong> and name it <strong>WallPainter</strong> and check <strong>Also create header file</strong>.</p><p>Finally select your app target folder (PaintApp Storyboard in my case) then click <strong>Next</strong> and <strong>Create</strong>. Xcode will then prompt you with some options to configure the app to use multiple languages. Click on <strong>Create Bridging Header option</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/0*ZNDbkuwU7ubIILYf" /></figure><p>The bridging header file is important as it will allow us to consume our Wall Painter algorithm by allowing different languages to talk to each other.</p><p>Open <strong>WallPainter.hpp</strong>, copy and paste the code below:</p><pre>#ifndef WallPainter_hpp</pre><pre>#define WallPainter_hpp</pre><pre>#endif /* WallPainter_hpp */</pre><pre>#include &lt;opencv2/opencv.hpp&gt;</pre><pre><strong>class</strong> WallPainter {</pre><pre><strong>public</strong>:</pre><pre>/*</pre><pre>Returns image with paint overlay</pre><pre>*/</pre><pre>  cv::Mat paint_wall(cv::Mat image,cv::Point p, cv::Size imageSize,cv::Scalar chosenColor);</pre><pre>  cv::Mat apply_texture(cv::Mat image,cv::Mat texture,cv::Point p, cv::Size imageSize);</pre><pre>};</pre><h3>Step 2: Add apply paint and Texture implementations.</h3><p>Open <strong>WallPainter.cpp</strong>, add the below methods:</p><h4>a) Paint Wall Method</h4><p>Painting the wall consist of the following steps:</p><ol><li>Get the Greyscale and HSV matrix for the given image</li><li>Get the S-channel matrix from the HSV image</li><li>Blur the image matrices</li><li>Do Canny Edge detection on both of them</li><li>Now, merge(linear/alpha blend) both the canny edge detected images</li><li>Dilate the resulting image matrix</li><li>Floodfill your original RGB image by taking the above resulting image as a mask and “tl(variable that stores the touch coordinates)” as your seed point with your chosen color</li><li>Now, again take the HSV matrix of your original image and merge the V-channel matrix into this flood-filled image</li><li>Finally add merge the resulting image with your original image.</li><li>Show your image to the user.</li></ol><p>Copy and paste the below code:</p><pre>#include “WallPainter.hpp”</pre><pre><strong>using</strong> <strong>namespace</strong> cv;</pre><pre><strong>using</strong> <strong>namespace</strong> std;</pre><pre>Mat WallPainter::paint_wall(Mat image,Point p, cv::Size imageSize, cv::Scalar chosenColor) {</pre><pre><strong>  double</strong> cannyMinThres = 30.0;</pre><pre><strong>  double</strong> ratio = 2.5;</pre><pre>  Mat mRgbMat = image;</pre><pre>  cvtColor(mRgbMat,mRgbMat,COLOR_RGBA2RGB);</pre><pre>  Mat mask = Mat(Size(mRgbMat.cols/8.0, mRgbMat.rows/8.0), CV_8UC1, Scalar(0.0));</pre><pre>  Mat img ;</pre><pre>  mRgbMat.copyTo(img);</pre><pre>  Mat mGreyScaleMat;</pre><pre>  cvtColor(mRgbMat, mGreyScaleMat,COLOR_RGB2GRAY, 3);</pre><pre>  medianBlur(mGreyScaleMat,mGreyScaleMat,3);</pre><pre>  Mat cannyGreyMat;</pre><pre>  Canny(mGreyScaleMat, cannyGreyMat, cannyMinThres, cannyMinThres*ratio, 3);</pre><pre>  //hsv</pre><pre>  Mat hsvImage;</pre><pre>  cvtColor(img,hsvImage,COLOR_RGB2HSV);</pre><pre>  //got the hsv values</pre><pre>  vector&lt;Mat&gt; list = vector&lt;Mat&gt;(3);</pre><pre>  split(hsvImage, list);</pre><pre>  Mat sChannelMat;</pre><pre>  vector&lt;Mat&gt; sList = vector&lt;Mat&gt;{list[1]};</pre><pre>  // sList.push_back(list[1]);</pre><pre>  merge(sList, sChannelMat);</pre><pre>  medianBlur(sChannelMat,sChannelMat,3);</pre><pre>  // canny</pre><pre>  Mat cannyMat;</pre><pre>  Canny(sChannelMat, cannyMat, cannyMinThres, cannyMinThres*ratio, 3);</pre><pre>  addWeighted(cannyMat,0.5, cannyGreyMat,0.5 ,0.0,cannyMat);</pre><pre>  dilate(cannyMat, cannyMat,mask, Point(0.0,0.0), 5);</pre><pre>  cout&lt;&lt;mRgbMat.cols&lt;&lt;”,”&lt;&lt;mRgbMat.rows&lt;&lt;”\n”;</pre><pre><strong>  double</strong> width = imageSize.width;</pre><pre><strong>  double</strong> height = imageSize.height;</pre><pre>  Point seedPoint = Point(p.x*(<strong>double</strong>(mRgbMat.cols)/width), p.y*(<strong>double</strong>(mRgbMat.rows)/height));</pre><pre>cout&lt;&lt;seedPoint.x&lt;&lt;”,”&lt;&lt;seedPoint.y&lt;&lt;”\n”;</pre><pre>// Make sure to resize the cannyMat or it&#39;ll throw an error</pre><pre>  resize(cannyMat, cannyMat, Size(cannyMat.cols + 2.0, cannyMat.rows + 2.0));</pre><pre>  medianBlur(mRgbMat,mRgbMat,15);</pre><pre><strong>  int</strong> floodFillFlag = 8;</pre><pre>  floodFill(mRgbMat,cannyMat,seedPoint,chosenColor,0,Scalar(5.0, 5.0, 5.0),Scalar(5.0, 5.0, 5.0),floodFillFlag);</pre><pre>  dilate(mRgbMat, mRgbMat, mask, Point(0.0,0.0), 5);</pre><pre>  //got the hsv of the mask image</pre><pre>  Mat rgbHsvImage;</pre><pre>  cvtColor(mRgbMat,rgbHsvImage,COLOR_RGB2HSV);</pre><pre>  vector&lt;Mat&gt; list1 = vector&lt;Mat&gt;(3);</pre><pre>  split(rgbHsvImage, list1);</pre><pre>  //merged the “v” of original image with mRgb mat</pre><pre>  Mat result;</pre><pre>  vector&lt;Mat&gt; newList = vector&lt;Mat&gt;();</pre><pre>  newList.push_back(list1[0]);</pre><pre>  newList.push_back(list1[1]);</pre><pre>  newList.push_back(list[2]);</pre><pre>  merge(newList, result);</pre><pre>  // converted to rgb</pre><pre>  cvtColor(result, result, COLOR_HSV2RGB);</pre><pre>  addWeighted(result,0.7, img,0.3 ,0.0,result);</pre><pre><strong>  return</strong> result;</pre><pre>}</pre><h4>b) Apply Texture Method</h4><p>Applying texture is similar to applying paint to your image:</p><ol><li>Follow Step 1 to 6 (given above) for your image.</li><li>For the floodfill part:</li></ol><p>a) wall-image: darken the area of the wall by taking the canny edge detected image as the mask.</p><p>b) texture image: brighten the area of the wall by taking the canny edge detected image as the mask.</p><p>3. Merge the two image matrices by using Bitwise_or operator on the two matrices.</p><p>4. Finally add the V-Channel of the original image and show it to the user.</p><p>Copy and paste the below code:</p><pre>Mat WallPainter::apply_texture(Mat image, Mat texture, Point p, cv::Size imageSize) {</pre><pre><strong>  double</strong> cannyMinThres = 30.0;</pre><pre><strong>  double</strong> ratio = 2.5;</pre><pre>  Mat mRgbMat = image;</pre><pre>  cvtColor(mRgbMat,mRgbMat,COLOR_RGBA2RGB);</pre><pre>  Mat mask = Mat(Size(mRgbMat.cols/8.0, mRgbMat.rows/8.0),CV_8UC1, Scalar(0.0));</pre><pre>  //Imgproc.dilate(mRgbMat, mRgbMat,mask, Point(0.0,0.0), 5)</pre><pre>  Mat img;</pre><pre>  mRgbMat.copyTo(img);</pre><pre>  //grayscale</pre><pre>  Mat mGreyScaleMat;</pre><pre>  cvtColor(mRgbMat, mGreyScaleMat,COLOR_RGB2GRAY, 3);</pre><pre>  medianBlur(mGreyScaleMat,mGreyScaleMat,3);</pre><pre>  Mat cannyGreyMat;</pre><pre>  Canny(mGreyScaleMat, cannyGreyMat, cannyMinThres,   cannyMinThres*ratio, 3);</pre><pre>  //hsv</pre><pre>  Mat hsvImage;</pre><pre>  cvtColor(img,hsvImage,COLOR_RGB2HSV);</pre><pre>  //got the hsv values</pre><pre>  vector&lt;Mat&gt; list = vector&lt;Mat&gt;(3);</pre><pre>  split(hsvImage, list);</pre><pre>  Mat sChannelMat;</pre><pre>  vector&lt;Mat&gt; sList = vector&lt;Mat&gt;{list[1]};</pre><pre>  merge(sList, sChannelMat);</pre><pre>  medianBlur(sChannelMat,sChannelMat,3);</pre><pre>  //canny</pre><pre>  Mat cannyMat;</pre><pre>  Canny(sChannelMat, cannyMat, cannyMinThres, cannyMinThres*ratio, 3);</pre><pre>  addWeighted(cannyMat,0.5, cannyGreyMat,0.5 ,0.0,cannyMat);</pre><pre>  dilate(cannyMat, cannyMat,mask, Point(0.0,0.0), 5);</pre><pre><strong>  double</strong> width = imageSize.width;</pre><pre><strong>  double</strong> height = imageSize.height;</pre><pre>  Point seedPoint = Point(p.x*(<strong>double</strong>(mRgbMat.cols)/width), p.y*(<strong>double</strong>(mRgbMat.rows)/height));</pre><pre>// Make sure to resize the cannyMat or it&#39;ll throw an error</pre><pre>  resize(cannyMat, cannyMat, Size(cannyMat.cols + 2.0, cannyMat.rows + 2.0));</pre><pre>  Mat cannyMat1;</pre><pre>  cannyMat.copyTo(cannyMat1);</pre><pre>  Mat wallMask = Mat(mRgbMat.size(),mRgbMat.type());</pre><pre><strong>  int</strong> floodFillFlag = 8;</pre><pre>  floodFill(wallMask,cannyMat,seedPoint,Scalar(255.0,255.0,255.0),0,Scalar(5.0, 5.0, 5.0),Scalar(5.0, 5.0, 5.0),floodFillFlag);</pre><pre>  //second floodfill is not working 5</pre><pre>  floodFill(mRgbMat,   cannyMat1,seedPoint,Scalar(0.0,0.0,0.0),0,Scalar(5.0, 5.0, 5.0),Scalar(5.0, 5.0, 5.0),floodFillFlag);</pre><pre>  Mat textureImgMat;</pre><pre>  cvtColor(texture,textureImgMat,COLOR_RGBA2RGB);</pre><pre>  resize(textureImgMat, textureImgMat, Size(mRgbMat.cols, mRgbMat.rows));</pre><pre>  bitwise_and(wallMask ,textureImgMat,textureImgMat);</pre><pre>  Mat resultImage;</pre><pre>  bitwise_or(textureImgMat,mRgbMat,resultImage);</pre><pre>  ////alpha blending</pre><pre>  //got the hsv of the mask image</pre><pre>  Mat rgbHsvImage;</pre><pre>  cvtColor(resultImage,rgbHsvImage,COLOR_RGB2HSV);</pre><pre>  vector&lt;Mat&gt; list1 = vector&lt;Mat&gt;(3);</pre><pre>  split(rgbHsvImage, list1);</pre><pre>  //merged the “v” of original image with mRgb mat</pre><pre>  Mat result;</pre><pre>  vector&lt;Mat&gt; newList = vector&lt;Mat&gt;();</pre><pre>  newList.push_back(list1[0]);</pre><pre>  newList.push_back(list1[1]);</pre><pre>  newList.push_back(list[2]);</pre><pre>  merge(newList, result);</pre><pre>  //converted to rgb</pre><pre>  cvtColor(result, result, COLOR_HSV2RGB);</pre><pre>  addWeighted(result,0.8, img,0.2 ,0.0,result);</pre><pre><strong>  return</strong> result;</pre><pre>}</pre><h3>Step 3: Consume Wall Painter algorithm using Swift</h3><p>Now, we need to consume the Wall Painter Algorithm code.</p><p>Our Swift code is not yet able to consume C++ code. However Objective-C is. Furthermore we can consume Objective-C code through Swift. To do that, let’s create Objective-C code to bridge between Swift and C++.</p><p>Add a new header file to the project. Select <strong>File &gt; New &gt; File…</strong> and then select <strong>Header file</strong> from the iOS template, name it <strong>WallPainterBridge</strong>(select your app target as the folder). Copy and paste the code below in <strong>WallPainterBridge.h</strong>:</p><pre>#import &lt;Foundation/Foundation.h&gt;</pre><pre>#import &lt;UIKit/UIKit.h&gt;</pre><pre><strong>@interface</strong> WallPainterBridge : NSObject</pre><pre>- (UIImage *) paintWallOf: (UIImage *) image touchPointX: (<strong>double</strong>) pointX touchPointY: (<strong>double</strong>) pointY imageWidth: (<strong>double</strong>) width imageHeight: (<strong>double</strong>) height colorToapply: (UIColor *) color;</pre><pre>- (UIImage *) applyTextureTo: (UIImage *) image textureToApply: (UIImage *) texture touchPointX: (<strong>double</strong>) pointX touchPointY: (<strong>double</strong>) pointY imageWidth: (<strong>double</strong>) width imageHeight: (<strong>double</strong>) height;</pre><pre><strong>@end</strong></pre><p>Next create an Objective-C file that will implement the WallPainterBridge interface. Select <strong>File &gt; New &gt; File.. </strong>and then select <strong>Objective-C File</strong> from iOS template. Name it <strong>WallPainterBridge </strong>(select your app target as the folder).</p><p>Edit the file name of the recently created <strong>WallPainterBridge.m</strong> and add an extra “m”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/226/0*MXoMd6luvVK1KLV8" /></figure><p>The extra m will tell Xcode that this is an Objective-C++ file. <strong>WallPainterBridge</strong> is now allowed to use C++ from within.</p><p>Copy and paste the code below to <strong>WallPainterBridge.mm</strong>:</p><pre>#import &lt;opencv2/opencv.hpp&gt;</pre><pre>#import &lt;opencv2/imgcodecs/ios.h&gt;</pre><pre>#import &lt;Foundation/Foundation.h&gt;</pre><pre>#import “WallPainterBridge.h”</pre><pre>#include “WallPainter.hpp”</pre><pre><strong>@implementation</strong> WallPainterBridge</pre><pre>- (UIImage *) paintWallOf:(UIImage *)image touchPointX:(<strong>double</strong>)pointX touchPointY:(<strong>double</strong>)pointY imageWidth:(<strong>double</strong>)width imageHeight:(<strong>double</strong>)height colorToapply:(UIColor*)color {</pre><pre>  //convert uiimage to mat</pre><pre>  cv::Mat opencvImage;</pre><pre>  UIImageToMat(image, opencvImage, <strong>true</strong>);</pre><pre>  //Run lane detection</pre><pre>  WallPainter wallPainter;</pre><pre>  cv::Point p = cv::Point(pointX,pointY);</pre><pre>  cv::Size imageSize = cv::Size(width,height);</pre><pre>  CGFloat red = 0;</pre><pre>  CGFloat green = 0;</pre><pre>  CGFloat blue = 0;</pre><pre>  CGFloat alpha = 0;</pre><pre>  [color getRed:&amp;red green:&amp;green blue:&amp;blue alpha:&amp;alpha];</pre><pre>  cv::Scalar chosenColor = cv::Scalar(red,green,blue,alpha);</pre><pre>  cv::Mat imageWithWallPainted =   wallPainter.paint_wall(opencvImage,p,imageSize,chosenColor);</pre><pre>  //convert mat to uiimage and return it to the caller</pre><pre><strong>  return</strong> MatToUIImage(imageWithWallPainted);</pre><pre>}</pre><pre>- (UIImage *) applyTextureTo:(UIImage *)image textureToApply:(UIImage *)texture touchPointX:(<strong>double</strong>)pointX touchPointY:(<strong>double</strong>)pointY imageWidth:(<strong>double</strong>)width imageHeight:(<strong>double</strong>)height {</pre><pre>  cv::Mat opencvImage;</pre><pre>  UIImageToMat(image, opencvImage, <strong>true</strong>);</pre><pre>  cv::Mat opencvTexture;</pre><pre>  UIImageToMat(texture, opencvTexture, <strong>true</strong>);</pre><pre>  //Run lane detection</pre><pre>  WallPainter wallPainter;</pre><pre>  cv::Point p = cv::Point(pointX,pointY);</pre><pre>  cv::Size imageSize = cv::Size(width,height);</pre><pre>  cv::Mat imageWithWallTextured =   wallPainter.apply_texture(opencvImage, opencvTexture, p, imageSize);</pre><pre>  //convert mat to uiimage and return it to the caller</pre><pre><strong>  return</strong> MatToUIImage(imageWithWallTextured);</pre><pre>}</pre><pre><strong>@end</strong></pre><p>Open (Your App name)-Bridging-Header.h and add the following line:</p><pre>#import “WallPainterBridge.h”</pre><h3>Coding your ViewController</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl0MYuZRGumMHcj2tG%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl0MYuZRGumMHcj2tG%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl0MYuZRGumMHcj2tG%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="239" frameborder="0" scrolling="no"><a href="https://medium.com/media/daff5da030996c24bd080d7ff8232817/href">https://medium.com/media/daff5da030996c24bd080d7ff8232817/href</a></iframe><h3>Step 1: Add a ColorPicker</h3><p>Now, we are gonna add ChromaColorPicker in our project.<br>(Here is the github repo for ChromaColorPicker:- <a href="https://github.com/joncardasis/ChromaColorPicker">https://github.com/joncardasis/ChromaColorPicker</a>)</p><p>Create a new folder inside Your app target folder and name it <strong>Chroma Color Picker</strong>.</p><p>Inside the folder add the following files from the ChromaColorPicker repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/0*4QRR_xmAV9o9K39n" /></figure><p>Now, go inside your <strong>PaintViewController </strong>and add a new colorPicker variable:</p><pre><strong>var</strong> colorPicker: ChromaColorPicker!</pre><p>Then add the chroma color picker delegate method:</p><pre>//MARK:<strong>- ChromaColorPicker delegate methods</strong></pre><pre><strong>extension</strong> PaintViewController: ChromaColorPickerDelegate {</pre><pre><strong>  func</strong> colorPickerDidChooseColor(<strong>_</strong> colorPicker: ChromaColorPicker, color: UIColor) {</pre><pre>    currentColor = color</pre><pre>    colorPicker.hexLabel.textColor = currentColor</pre><pre>    colorPicker.isHidden = <strong>true</strong></pre><pre>  }</pre><pre>}</pre><h3>Step 2: Add Image Picker Delegate</h3><p>Copy and paste the below lines of code:</p><pre>//MARK:<strong>- ImagePicker delegate methods</strong></pre><pre><strong>extension</strong> PaintViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {</pre><pre><strong>  func</strong> imagePickerController(<strong>_</strong> picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : <strong>Any</strong>]) {</pre><pre>    picker.dismiss(animated: <strong>true</strong>, completion: <strong>nil</strong>)</pre><pre><strong>    guard</strong> <strong>let</strong> image = info[.editedImage] <strong>as</strong>? UIImage <strong>else</strong> {</pre><pre>    print(“no image found”)</pre><pre><strong>    return</strong></pre><pre> }</pre><pre> print(image)</pre><pre> imageView.image = image</pre><pre> }</pre><pre>}</pre><h3>Step 3: Add get photo method</h3><p>Copy and paste the below lines of code inside <strong>PaintViewController</strong>:</p><pre>//MARK:<strong>- Custom Methods</strong></pre><pre><strong>private</strong> <strong>func</strong> getPhoto() {</pre><pre><strong>  if</strong> cameraToggled {</pre><pre>    imagePickerVC.sourceType = .camera</pre><pre>  } <strong>else</strong> {</pre><pre>    imagePickerVC.sourceType = .photoLibrary</pre><pre>  }</pre><pre>  present(imagePickerVC, animated: <strong>true</strong>)</pre><pre>}</pre><h3>Step 4: Code the onCreate method</h3><p>Copy and paste the below lines of code inside the <strong>onCreate</strong> method:</p><pre>//MARK:<strong>- Lifecycle Hooks</strong></pre><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {</pre><pre><strong>  super</strong>.viewDidLoad()</pre><pre>  print(“success”)</pre><pre>  colorPicker = ChromaColorPicker(frame: CGRect(x: view.frame.width/2–140, y: view.frame.height/2–270, width: 300, height: 300))</pre><pre>  colorPicker.delegate = <strong>self</strong> //ChromaColorPickerDelegate</pre><pre>  colorPicker.padding = 5</pre><pre>  colorPicker.stroke = 3</pre><pre>  colorPicker.currentColor = currentColor</pre><pre>  colorPicker.hexLabel.textColor = currentColor</pre><pre>  colorPicker.isHidden = <strong>true</strong></pre><pre>  view.addSubview(colorPicker)</pre><pre>  getPhoto()</pre><pre>}</pre><h3>Final result</h3><p>Now, after getting the image and tapping on the wall inside the image the wall should be painted red (red is the default color, of course you can choose other colours and your texture from the Toolbar menu).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*gCu1qrV2SgoegBb3" /></figure><h3>Goodbye</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fd31w24psGYeekCZy%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Famericasgottalent-agt-americas-got-talent-d31w24psGYeekCZy&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExeGY2eDkxbHBwNzlmOTNwYTh1N2NuamxrY3V3YmJyaGZ4NnU1M3l2ZiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2Fd31w24psGYeekCZy%2Fgiphy_s.gif&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/f98596e3a19e831ae9a6e230bd91b978/href">https://medium.com/media/f98596e3a19e831ae9a6e230bd91b978/href</a></iframe><p>Congratulations!!! , you painted the wall inside the image using OpenCV libraries.</p><p>And with that my friend, you are done. Great job!</p><p>You can find the whole code for this app here : <a href="https://github.com/tushar40/Wall-Paint-App-iOS/tree/master/Paint%20app%20StoryboardUI">https://github.com/tushar40/Wall-Paint-App-iOS/tree/master/Paint%20app%20StoryboardUI</a></p><h3>References:-</h3><p><a href="https://medium.com/onfido-tech/building-a-simple-lane-detection-ios-app-using-opencv-4f70d8a6e6bc">https://medium.com/onfido-tech/building-a-simple-lane-detection-ios-app-using-opencv-4f70d8a6e6bc</a></p><p><a href="https://medium.com/@tushargusain40/using-opencv-in-android-studio-to-paint-the-wall-in-an-image-abac3a79c790">https://medium.com/@tushargusain40/using-opencv-in-android-studio-to-paint-the-wall-in-an-image-abac3a79c790</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0ceffed949b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using OpenCV in Android studio to paint the wall in an image]]></title>
            <link>https://tushargusain40.medium.com/using-opencv-in-android-studio-to-paint-the-wall-in-an-image-abac3a79c790?source=rss-246d3cfcb899------2</link>
            <guid isPermaLink="false">https://medium.com/p/abac3a79c790</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[android-studio]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[paint]]></category>
            <dc:creator><![CDATA[Tushar Gusain]]></dc:creator>
            <pubDate>Sun, 10 May 2020 10:20:06 GMT</pubDate>
            <atom:updated>2021-02-13T08:23:34.785Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn about Android Camera, OpenCV, and storing images in this Android Studio Tutorial.</h4><h3>Version:-</h3><p>Android Studio 3.x, kotlin 1.3.x</p><p>This is a post by Tushar Gusain, an Android and iOS developer.</p><p>A good paint job beautifies and adds character and personality to your home. Just for that reason it’s important to take sufficient time planning which colours, shades, and paint styles you want to decorate your home with.</p><p>Whenever you buy a new house, whitewash your home, or just want to repaint it you are stuck at deciding which colour will suit best for the walls. Making an app that’ll help you visualise those walls in different colours will come in handy.</p><p>We can do this by taking a photo of one or more walls with your phone and clicking the wall to paint them in the chosen colour or texture. Painting the wall, i.e. image processing part can be done with the help of OpenCV library.</p><p>Keep reading to know how simple it is to integrate OpenCV in your app.</p><h3>About OpenCV</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rJWE9jCWSIX6nTGT" /></figure><p><strong>OpenCV</strong> (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel).</p><p>It is an open source library with quite a number of useful Algorithms and API which can be used for image processing. It is mainly used to do all the operation related to Images, like:</p><p><strong>1. </strong>Reading and Writing Images.</p><p>2. Detecting faces and its features.</p><p>3. Detecting shapes in an image.</p><p>4. Text recognition.</p><p>5. Modifying image quality and colours.</p><p>6. Developing Augmented reality apps.</p><p>Etc…..</p><p>OpenCV supports a wide variety of programming languages such as C++, Python, Java, etc., and is available on different platforms including Windows, Linux, OS X, Android, and iOS. Interfaces for high-speed GPU operations based on CUDA and OpenCL are also under active development.</p><p>( To know more about OpenCV follow this link: <a href="https://opencv.org/about/">https://opencv.org/about/</a> )</p><h3>Creating your Android Studio project</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FSlimTheseAfricanbushviper&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fthumbs.gfycat.com%2FSlimTheseAfricanbushviper-mobile.mp4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="1068" height="744" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c3eaead52ae428a176e83417723c91e/href">https://medium.com/media/0c3eaead52ae428a176e83417723c91e/href</a></iframe><p>Open <strong>Android Studio</strong>, click on <strong>Start a new project</strong>, then choose an <strong>Empty Activity</strong> and click <strong>Next</strong>.</p><p>Now, Enter the name of your App in the <strong>Name</strong> field, then, select <strong>Kotlin </strong>as your Language and click on <strong>Finish</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/0*6uJwhnYKJWO2v-xA" /></figure><h3>Integrating the OpenCV module</h3><h3>Step 1: Downloading OpenCV</h3><p>Download the OpenCV library from here: <a href="https://opencv.org/releases/">https://opencv.org/releases/<br></a>For this project I am using OpenCV — 3.4.7, after downloading unzip the folder.</p><h3>Step 2: Importing OpenCV</h3><p>Now, inside Android Studio , <strong>Select File &gt; New &gt; import module,</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A15hsXbN-iRE5RNe" /></figure><p>then, for the source directory select<strong> OpenCV-android-sdk(the unzipped folder) &gt; sdk &gt; java.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ynXwnZGt4VqKngJe" /></figure><p>After this click <strong>Next</strong> then click<strong> Finish.</strong> This will make an openCVLibrary347 module for you.</p><h3>Step 3: Fixing the error</h3><p>Now if you build the project you’ll get an error, to fix this error:<br>Inside the <strong>openCVLibrary347.build.gradle</strong> change the <strong>compileSdkVersion</strong></p><p>and <strong>defaultConfig.minSdkVersion</strong> to be same as that of app.build.gradle, then</p><h3>Step 4: Add OpenCV dependency</h3><p>To work with the OpenCV Android library, you have to add it to your app module as a dependency. To easily do this click on <strong>File &gt; Project Structure</strong>, click on the <strong>app</strong> module.</p><p>After navigating to the module, click on the <strong>Dependencies</strong> tab, click the “<strong>+</strong>” button select <strong>Module Dependency</strong>, then select <strong>OpenCVLibrary347</strong> and then press <strong>OK</strong>.<br>After that click <strong>Apply</strong> then press <strong>OK</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lSrNIdcFeh1yzteg" /></figure><h3>Step 6: Add Native Libraries</h3><p>Now navigate to the unzipped folder of the OpenCV Android library. Open the <strong>sdk &gt; native </strong>folder then copy the <strong>libs </strong>folder inside the <strong>native</strong> folder and paste it to <strong>PaintApp (your project name) &gt; src &gt; main</strong> folder.</p><p>After that rename <strong>libs</strong> to <strong>jniLibs</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/0*ddwMSd4utRUq7fOQ" /></figure><h3>Building the User Interface</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FDisastrousFatherlyJapanesebeetle&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fthumbs.gfycat.com%2FDisastrousFatherlyJapanesebeetle-mobile.mp4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="1920" height="1080" frameborder="0" scrolling="no"><a href="https://medium.com/media/911da1bef87b57164c3145eee146878b/href">https://medium.com/media/911da1bef87b57164c3145eee146878b/href</a></iframe><h3>Step 1: Add a layout to your activity_main.xml</h3><p>Inside your <strong>activity_main.xml</strong> add the following layout</p><pre>&lt;<strong>LinearLayout</strong></pre><pre><strong> xmlns:android=”http://schemas.android.com/apk/res/android&quot;</strong></pre><pre><strong> xmlns:tools=”http://schemas.android.com/tools&quot;</strong></pre><pre><strong> android:layout_width=”match_parent”</strong></pre><pre><strong> android:layout_height=”match_parent”</strong></pre><pre><strong> android:orientation=”vertical”</strong></pre><pre><strong> tools:context=”.MainActivity”</strong>&gt;</pre><pre> &lt;<strong>ImageView</strong></pre><pre><strong> android:id=”@+id/imageFromData”</strong></pre><pre><strong> android:layout_width=”match_parent”</strong></pre><pre><strong> android:layout_height=”match_parent”</strong></pre><pre><strong> android:visibility=”visible”</strong>/&gt;</pre><pre> &lt;<strong>LinearLayout</strong></pre><pre><strong>  android:id=”@+id/topLayout”</strong></pre><pre><strong>  android:layout_width=”match_parent”</strong></pre><pre><strong>  android:layout_height=”0dp”</strong></pre><pre><strong>  android:layout_weight=”1&quot;</strong></pre><pre><strong>  android:orientation=”horizontal”</strong></pre><pre><strong>  android:visibility=”gone”</strong>&gt;</pre><pre>  &lt;<strong>RelativeLayout</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”0dp”</strong></pre><pre><strong>   android:layout_weight=”1&quot;</strong>&gt;</pre><pre>   &lt;<strong>ImageView</strong></pre><pre><strong>    android:id=”@+id/inputImage”</strong></pre><pre><strong>    android:layout_height=”match_parent”</strong></pre><pre><strong>    android:layout_width=”match_parent”</strong></pre><pre><strong>    android:scaleType=”fitXY” </strong>/&gt;</pre><pre>   &lt;<strong>TextView</strong></pre><pre><strong>    android:layout_width=”wrap_content”</strong></pre><pre><strong>    android:layout_height=”wrap_content”</strong></pre><pre><strong>    android:text=”Input Image”</strong>/&gt;</pre><pre>  &lt;/<strong>RelativeLayout</strong>&gt;</pre><pre>  &lt;<strong>RelativeLayout</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”0dp”</strong></pre><pre><strong>   android:layout_weight=”1&quot;</strong>&gt;</pre><pre>   &lt;<strong>ImageView</strong></pre><pre><strong>   android:id=”@+id/greyScaleImage”</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”match_parent”</strong></pre><pre><strong>   android:scaleType=”fitXY” </strong>/&gt;</pre><pre>   &lt;<strong>TextView</strong></pre><pre><strong>    android:layout_width=”wrap_content”</strong></pre><pre><strong>    android:layout_height=”wrap_content”</strong></pre><pre><strong>    android:text=”Grey Scale Image”</strong>/&gt;</pre><pre>  &lt;/<strong>RelativeLayout</strong>&gt;</pre><pre> &lt;/<strong>LinearLayout</strong>&gt;</pre><pre> &lt;<strong>LinearLayout</strong></pre><pre><strong>  android:id=”@+id/middleLayout”</strong></pre><pre><strong>  android:layout_width=”match_parent”</strong></pre><pre><strong>  android:layout_height=”0dp”</strong></pre><pre><strong>  android:layout_weight=”1&quot;</strong></pre><pre><strong>  android:orientation=”horizontal”</strong></pre><pre><strong>  android:visibility=”gone”</strong>&gt;</pre><pre>  &lt;<strong>RelativeLayout</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”0dp”</strong></pre><pre><strong>   android:layout_weight=”1&quot;</strong>&gt;</pre><pre>   &lt;<strong>ImageView</strong></pre><pre><strong>    android:id=”@+id/cannyEdgeImage”</strong></pre><pre><strong>    android:layout_height=”match_parent”</strong></pre><pre><strong>    android:layout_width=”match_parent”</strong></pre><pre><strong>    android:scaleType=”fitXY” </strong>/&gt;</pre><pre>   &lt;<strong>TextView</strong></pre><pre><strong>    android:layout_width=”wrap_content”</strong></pre><pre><strong>    android:layout_height=”wrap_content”</strong></pre><pre><strong>    android:textColor=”#FF0000&quot;</strong></pre><pre><strong>    android:text=”Canny Edge Mask”</strong>/&gt;</pre><pre>  &lt;/<strong>RelativeLayout</strong>&gt;</pre><pre>  &lt;<strong>RelativeLayout</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”0dp”</strong></pre><pre><strong>   android:layout_weight=”1&quot;</strong>&gt;</pre><pre>   &lt;<strong>ImageView</strong></pre><pre><strong>    android:id=”@+id/floodFillImage”</strong></pre><pre><strong>    android:layout_height=”match_parent”</strong></pre><pre><strong>    android:layout_width=”match_parent”</strong></pre><pre><strong>    android:scaleType=”fitXY” </strong>/&gt;</pre><pre>   &lt;<strong>TextView</strong></pre><pre><strong>    android:layout_width=”wrap_content”</strong></pre><pre><strong>    android:layout_height=”wrap_content”</strong></pre><pre><strong>    android:text=”Flood Fill Image”</strong>/&gt;</pre><pre>  &lt;/<strong>RelativeLayout</strong>&gt;</pre><pre> &lt;/<strong>LinearLayout</strong>&gt;</pre><pre> &lt;<strong>LinearLayout</strong></pre><pre><strong>  android:id=”@+id/bottomLayout”</strong></pre><pre><strong>  android:layout_width=”match_parent”</strong></pre><pre><strong>  android:layout_height=”0dp”</strong></pre><pre><strong>  android:layout_weight=”1&quot;</strong></pre><pre><strong>  android:orientation=”horizontal”</strong></pre><pre><strong>  android:visibility=”gone”</strong>&gt;</pre><pre>  &lt;<strong>RelativeLayout</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”0dp”</strong></pre><pre><strong>   android:layout_weight=”1&quot;</strong>&gt;</pre><pre>   &lt;<strong>ImageView</strong></pre><pre><strong>    android:id=”@+id/HSVImage”</strong></pre><pre><strong>    android:layout_height=”match_parent”</strong></pre><pre><strong>    android:layout_width=”match_parent”</strong></pre><pre><strong>    android:scaleType=”fitXY” </strong>/&gt;</pre><pre>   &lt;<strong>TextView</strong></pre><pre><strong>    android:layout_width=”wrap_content”</strong></pre><pre><strong>    android:layout_height=”wrap_content”</strong></pre><pre><strong>    android:text=”HSV Image”</strong>/&gt;</pre><pre>  &lt;/<strong>RelativeLayout</strong>&gt;</pre><pre>  &lt;<strong>RelativeLayout</strong></pre><pre><strong>   android:layout_height=”match_parent”</strong></pre><pre><strong>   android:layout_width=”0dp”</strong></pre><pre><strong>   android:layout_weight=”1&quot;</strong>&gt;</pre><pre>   &lt;<strong>ImageView</strong></pre><pre><strong>    android:id=”@+id/outputImage” </strong></pre><pre><strong>    android:layout_height=”match_parent”</strong></pre><pre><strong>    android:layout_width=”match_parent”</strong></pre><pre><strong>    android:scaleType=”fitXY” </strong>/&gt;</pre><pre>   &lt;<strong>TextView</strong></pre><pre><strong>    android:layout_width=”wrap_content”</strong></pre><pre><strong>    android:layout_height=”wrap_content”</strong></pre><pre><strong>    android:text=”Output Image”</strong>/&gt;</pre><pre>  &lt;/<strong>RelativeLayout</strong>&gt;</pre><pre> &lt;/<strong>LinearLayout</strong>&gt;</pre><pre>&lt;/<strong>LinearLayout</strong>&gt;</pre><h3>Step 2: Add strings</h3><p>Add the following strings to your strings.xml file</p><pre>&lt;<strong>string name=”menu_open”</strong>&gt;Final Image&lt;/<strong>string</strong>&gt;</pre><pre>&lt;<strong>string name=”menu_select”</strong>&gt;Select target&lt;/<strong>string</strong>&gt;</pre><pre>&lt;<strong>string name=”menu_cut”</strong>&gt;Process Image&lt;/<strong>string</strong>&gt;</pre><pre>&lt;<strong>string name=”menu_photo”</strong>&gt;Take Photo&lt;/<strong>string</strong>&gt;</pre><pre>&lt;<strong>string name=”menu_gallery”</strong>&gt;Open Gallery&lt;/<strong>string</strong>&gt;</pre><pre>&lt;<strong>string name=”menu_color”</strong>&gt;Choose Color&lt;/<strong>string</strong>&gt;</pre><pre>&lt;<strong>string name=”menu_texture”</strong>&gt;Choose Texture&lt;/<strong>string</strong>&gt;</pre><h3>Step 3: Add menu</h3><p>Right click on <strong>res</strong> folder select <strong>New &gt; Android Resource File , </strong>Give <strong>File name </strong>as <em>menu_main </em>and <strong>Resource type </strong>as <strong>menu</strong></p><p>Now, add the following lines to your main_menu.xml file</p><pre>&lt;<strong>item android:id=”@+id/action_open_img”</strong></pre><pre><strong> android:title=”@string/menu_open”</strong></pre><pre><strong> android:orderInCategory=”1&quot;</strong></pre><pre><strong> app:showAsAction=”never” </strong>/&gt;</pre><pre>&lt;<strong>item android:id=”@+id/action_process_image”</strong></pre><pre><strong> android:title=”@string/menu_cut”</strong></pre><pre><strong> android:orderInCategory=”2&quot;</strong></pre><pre><strong> app:showAsAction=”never” </strong>/&gt;</pre><pre>&lt;<strong>item android:id=”@+id/action_take_photo”</strong></pre><pre><strong> android:title=”@string/menu_photo”</strong></pre><pre><strong> android:orderInCategory=”3&quot;</strong></pre><pre><strong> app:showAsAction=”never” </strong>/&gt;</pre><pre>&lt;<strong>item android:id=”@+id/action_get_gallery”</strong></pre><pre><strong> android:title=”@string/menu_gallery”</strong></pre><pre><strong> android:orderInCategory=”4&quot;</strong></pre><pre><strong> app:showAsAction=”never” </strong>/&gt;</pre><pre>&lt;<strong>item android:id=”@+id/action_get_color”</strong></pre><pre><strong> android:title=”@string/menu_color”</strong></pre><pre><strong> android:orderInCategory=”5&quot;</strong></pre><pre><strong> app:showAsAction=”never” </strong>/&gt;</pre><pre>&lt;<strong>item android:id=”@+id/action_get_texture”</strong></pre><pre><strong> android:title=”@string/menu_texture”</strong></pre><pre><strong> android:orderInCategory=”6&quot;</strong></pre><pre><strong> app:showAsAction=”never” </strong>/&gt;</pre><h3>Step 4: Add a texture image</h3><p>To use as a texture image download any png of a texture from internet, i have used the following image as a texture and named it <strong>texture_small_brick_red.jpg </strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xUqnQfSlN4x5r8Nz" /></figure><h3>Coding Permissions and Activity</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FFlimsyFatGlowworm&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fthumbs.gfycat.com%2FFlimsyFatGlowworm-mobile.mp4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="740" height="416" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2bb2b5ec74c86acda4cd5a6ea2ca4cd/href">https://medium.com/media/a2bb2b5ec74c86acda4cd5a6ea2ca4cd/href</a></iframe><h3>Step 1: Add permissions</h3><p>In your AndroidManifest.xml add the following lines</p><pre>&lt;<strong>uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”</strong>/&gt;</pre><pre>&lt;<strong>uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”</strong>/&gt;</pre><pre>&lt;<strong>uses-permission android:name=”android.permission.CAMERA”</strong>/&gt;</pre><pre>&lt;<strong>application</strong></pre><pre><strong> android:allowBackup=”true”</strong></pre><pre><strong> android:icon=”@mipmap/ic_launcher”</strong></pre><pre><strong> android:label=”@string/app_name”</strong></pre><pre><strong> android:roundIcon=”@mipmap/ic_launcher_round”</strong></pre><pre><strong> android:supportsRtl=”true”</strong></pre><pre><strong> android:theme=”@style/AppTheme”</strong>&gt;</pre><pre>&lt;<strong>activity android:name=”.MainActivity”</strong></pre><pre><strong>  android:hardwareAccelerated=”true”</strong>&gt;</pre><pre>  &lt;<strong>intent-filter</strong>&gt;</pre><pre>   &lt;<strong>action android:name=”android.intent.action.MAIN” </strong>/&gt;</pre><pre>   &lt;<strong>action android:name=”android.intent.action.VIEW”</strong>/&gt;</pre><pre>   &lt;<strong>category android:name=”android.intent.category.LAUNCHER” </strong>/&gt;</pre><pre>  &lt;/<strong>intent-filter</strong>&gt;</pre><pre> &lt;/<strong>activity</strong>&gt;</pre><pre> &lt;<strong>provider</strong></pre><pre><strong>  android:name=”androidx.core.content.FileProvider”</strong></pre><pre><strong>  android:authorities=”${applicationId}.provider”</strong></pre><pre><strong>  android:exported=”false”</strong></pre><pre><strong>  android:grantUriPermissions=”true”</strong>&gt;</pre><pre>  &lt;<strong>meta-data</strong></pre><pre><strong>   android:name=”android.support.FILE_PROVIDER_PATHS”</strong></pre><pre><strong>   android:resource=”@xml/provider_paths”</strong>/&gt;</pre><pre> &lt;/<strong>provider</strong>&gt;</pre><pre>&lt;/<strong>application</strong>&gt;</pre><h3>Step 2: Add the provider_paths.xml file</h3><p>Right click on <strong>res</strong> folder select <strong>New &gt; Android Resource File , </strong>Give <strong>File name </strong>as <em>provider_paths </em>and <strong>Resource type </strong>as <strong>xml</strong></p><p>Now, add the following lines to your provider_paths.xml file</p><pre>&lt;<strong>paths xmlns:android=”http://schemas.android.com/apk/res/android&quot;</strong>&gt;</pre><pre> &lt;<strong>external-path name=”external_files”</strong></pre><pre><strong>  path=”Android/data/com.example.paintapp/files/Pictures”</strong>/&gt;</pre><pre> &lt;/<strong>paths</strong>&gt;</pre><h3>Step 3: Add dependencies</h3><p>Add the following dependencies in your <strong>App build.gradle</strong></p><pre><strong>dependencies {</strong></pre><pre><strong>implementation fileTree(dir: ‘libs’, include: [‘*.jar’])</strong></pre><pre><strong>implementation “org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version”</strong></pre><pre><strong>implementation ‘androidx.appcompat:appcompat:1.0.2’</strong></pre><pre><strong>implementation ‘androidx.core:core-ktx:1.0.2’</strong></pre><pre><strong>implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’</strong></pre><pre><strong>testImplementation ‘junit:junit:4.12’</strong></pre><pre><strong>androidTestImplementation ‘androidx.test:runner:1.1.1’</strong></pre><pre><strong>androidTestImplementation ‘androidx.test.espresso:espresso-core:3.1.1’</strong></pre><pre><strong>implementation project(path: ‘:openCVLibrary347’)</strong></pre><pre><strong>implementation ‘com.github.yukuku:ambilwarna:2.0.1’ // color picker</strong></pre><pre><strong>}</strong></pre><h3>Step 4: Adding Property Variables and Models</h3><p>Now, add the following lines of code to your <strong>MainActivity</strong></p><pre><strong>private enum class </strong>LoadImage {</pre><pre><strong> PICK_FROM_CAMERA</strong>,</pre><pre><strong> PICK_FROM_GALLERY</strong></pre><pre>}</pre><pre><strong>companion object </strong>{</pre><pre><strong> init </strong>{</pre><pre>  System.loadLibrary(<strong>“opencv_java”</strong>)</pre><pre> }</pre><pre>}</pre><pre><strong>var touchCount </strong>= 0</pre><pre><strong>lateinit var tl</strong>: Point</pre><pre><strong>lateinit var bitmap</strong>: Bitmap</pre><pre><strong>var chosenColor </strong>= Color.<em>RED</em></pre><pre><strong>private lateinit var imageFilePath</strong>: String</pre><pre><strong>private var texture </strong>= <strong>false</strong></pre><pre><strong>private val TAG </strong>= MainActivity::<strong>class</strong>.<em>java</em>.<em>simpleName</em></pre><pre><strong>private val PERMISSIONS </strong>= <em>arrayOf</em>&lt;String&gt;(</pre><pre> Manifest.permission.<em>READ_EXTERNAL_STORAGE</em>,</pre><pre> Manifest.permission.<em>WRITE_EXTERNAL_STORAGE</em>,</pre><pre> Manifest.permission.<em>CAMERA</em></pre><pre>)</pre><h3>Step 5: Setting up rest of the activity</h3><h4>Override OnCreate Method</h4><p>Add the following lines inside your <strong>onCreate</strong> method</p><pre><strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {</pre><pre><strong> super</strong>.onCreate(savedInstanceState)</pre><pre> setContentView(R.layout.<em>activity_main</em>)</pre><pre><strong> tl </strong>= Point()</pre><pre> openCamera()</pre><pre>}</pre><p>This’ll initialize your tl(variable to store your touch point) and open camera as soon as the activity is created.</p><h4>Override onCreateOptionsMenu Method</h4><p>Add the these lines to your <strong>onCreateOptionsMenu</strong> method</p><pre><strong>override fun </strong>onCreateOptionsMenu(menu: Menu?): Boolean {</pre><pre><em> menuInflater</em>.inflate(R.menu.<em>menu_main</em>,menu)</pre><pre><strong> return super</strong>.onCreateOptionsMenu(menu)</pre><pre>}</pre><p>This’ll inflate your menu_main file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/0*BTaMeXAUzhzv4kPN" /></figure><h4>Override onOptionsItemSelected Method</h4><p>Add the these lines to your <strong>onOptionsItemSelected</strong> method</p><pre><strong>override fun </strong>onOptionsItemSelected(item: MenuItem): Boolean {</pre><pre><strong> when</strong>(item.<em>itemId</em>) {</pre><pre>  R.id.<em>action_open_img </em>-&gt; {</pre><pre>   showImage()</pre><pre>  }</pre><pre>  R.id.<em>action_process_image </em>-&gt; {</pre><pre>   showResultLayouts()</pre><pre>  }</pre><pre>  R.id.<em>action_take_photo </em>-&gt; {</pre><pre>   openCamera()</pre><pre>  }</pre><pre>  R.id.<em>action_get_gallery </em>-&gt; {</pre><pre>   openGallery()</pre><pre>  }</pre><pre>  R.id.<em>action_get_color </em>-&gt; {</pre><pre>   chooseColor()</pre><pre>  }</pre><pre>  R.id.<em>action_get_texture </em>-&gt; {</pre><pre>   chooseTexture()</pre><pre>  }</pre><pre> }</pre><pre><strong> return true</strong></pre><pre>}</pre><p>This’ll call the respective functions for your menu items when they are selected.</p><h4>Override onActivityResult Method</h4><p>Add the these lines to your <strong>onActivityResult</strong> method</p><pre><strong>public override fun </strong>onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {</pre><pre><strong> super</strong>.onActivityResult(requestCode, resultCode, data)</pre><pre><strong> when </strong>(requestCode) {</pre><pre>  LoadImage.<strong>PICK_FROM_CAMERA</strong>.<strong>ordinal </strong>-&gt; <strong>if </strong>(resultCode ==   Activity.<em>RESULT_OK</em>) {</pre><pre><strong>   try </strong>{</pre><pre>    imageFromData.setImageURI(Uri.parse(<strong>imageFilePath</strong>))</pre><pre><strong>    bitmap </strong>= imageFromData.<em>drawable</em>.<em>toBitmap</em>()</pre><pre><strong>    bitmap </strong>= getResizedBitmap(<strong>bitmap</strong>,<strong>bitmap</strong>.<em>width</em>/5,<strong>bitmap</strong>.<em>height</em>/5)</pre><pre>    showImage()</pre><pre>   } <strong>catch </strong>(e: IOException) {</pre><pre>    e.printStackTrace()</pre><pre>   }</pre><pre>  }</pre><pre>  LoadImage.<strong>PICK_FROM_GALLERY</strong>.<strong>ordinal </strong>-&gt; <strong>if </strong>(resultCode == Activity.<em>RESULT_OK</em>) {</pre><pre>   loadFromGallery(data)</pre><pre>  }</pre><pre> }</pre><pre> imageFromData.setOnTouchListener(<strong>object </strong>: View.OnTouchListener {</pre><pre><strong>  override fun </strong>onTouch(v: View, event: MotionEvent): Boolean {</pre><pre><strong>   if </strong>(event.<em>action </em>== MotionEvent.<em>ACTION_DOWN</em>) {</pre><pre><strong>    if </strong>(<strong>touchCount </strong>== 0) {</pre><pre><strong>     tl</strong>.<strong>x </strong>= event.<em>x</em>.toDouble()</pre><pre><strong>     tl</strong>.<strong>y </strong>= event.<em>y</em>.toDouble()</pre><pre><strong>     if</strong>(<strong>texture</strong>) {</pre><pre>      applyTexture(<strong>bitmap</strong>, <strong>tl</strong>)</pre><pre>     } <strong>else </strong>{</pre><pre>      rpPaintHSV(<strong>bitmap</strong>,<strong>tl</strong>)</pre><pre>     }</pre><pre>    }</pre><pre>   }</pre><pre><strong>   return true</strong></pre><pre>  } </pre><pre> })</pre><pre>}</pre><p>This’ll load the image taken from your camera/gallery into the imageview and add an setOnTouchListener to it , so you can paint the wall you touched in the image.</p><h4>Override onRequestPermissionsResult Method</h4><p>Add the these lines to your <strong>onRequestPermissionsResult</strong> method</p><pre><strong>override fun </strong>onRequestPermissionsResult(requestCode: Int, permissions: Array&lt;String&gt;, grantResults: IntArray) {</pre><pre><strong> when </strong>(requestCode) {</pre><pre>  LoadImage.<strong>PICK_FROM_CAMERA</strong>.<strong>ordinal </strong>-&gt; {</pre><pre><strong>   if </strong>(grantResults.<em>isEmpty</em>() || grantResults[0] != PackageManager.<em>PERMISSION_GRANTED</em>) {</pre><pre>   Log.e(<strong>TAG</strong>, <strong>“Permission has been denied by user”</strong>)</pre><pre>   } <strong>else </strong>{</pre><pre>    openCamera()</pre><pre>    Log.e(<strong>TAG</strong>, <strong>“Permission has been granted by user”</strong>)</pre><pre>   }</pre><pre>  }</pre><pre> }</pre><pre>}</pre><p>This’ll open the camera once you give the permissions in your app.</p><h3>Storing and loading the image</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FBriefBrilliantDegus&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fthumbs.gfycat.com%2FBriefBrilliantDegus-mobile.mp4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="272" height="60" frameborder="0" scrolling="no"><a href="https://medium.com/media/17fe75e15bebcf5a17583f29eb3c1405/href">https://medium.com/media/17fe75e15bebcf5a17583f29eb3c1405/href</a></iframe><h3>Step 1: Add helper methods</h3><p>Add the below helper methods to <strong>MainActivity,</strong></p><pre><strong>private fun </strong>showImage(image: Mat, view: ImageView) {</pre><pre><strong> val </strong>mBitmap = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.<strong>ARGB_8888</strong>);</pre><pre> Utils.matToBitmap(image, mBitmap)</pre><pre> view.setImageBitmap(mBitmap)</pre><pre><strong> bitmap </strong>= mBitmap</pre><pre> saveImage(<strong>bitmap</strong>)</pre><pre>}</pre><pre><strong>private fun </strong>showResultLayouts() {</pre><pre> imageFromData.<em>visibility </em>= View.<em>GONE</em></pre><pre> topLayout.<em>visibility </em>= View.<em>VISIBLE</em></pre><pre> middleLayout.<em>visibility </em>= View.<em>VISIBLE</em></pre><pre> bottomLayout.<em>visibility </em>= View.<em>VISIBLE</em></pre><pre>}</pre><pre><strong>private fun </strong>showImage() {</pre><pre> imageFromData.<em>visibility </em>= View.<em>VISIBLE</em></pre><pre> topLayout.<em>visibility </em>= View.<em>GONE</em></pre><pre> middleLayout.<em>visibility </em>= View.<em>GONE</em></pre><pre> bottomLayout.<em>visibility </em>= View.<em>GONE</em></pre><pre><strong> try </strong>{</pre><pre>  imageFromData.setImageBitmap(<strong>bitmap</strong>)</pre><pre> } <strong>catch </strong>(e: Exception) {</pre><pre>  Toast.makeText(<strong>this</strong>@MainActivity, <strong>“No image selected”</strong>,Toast.<em>LENGTH_SHORT</em>).show()</pre><pre> }</pre><pre>}</pre><pre><strong>private fun </strong>chooseColor() {</pre><pre><strong> texture </strong>= <strong>false</strong></pre><pre><strong> val </strong>colorPicker = AmbilWarnaDialog(<strong>this</strong>@MainActivity, <strong>chosenColor</strong>,  <strong>object</strong>: AmbilWarnaDialog.OnAmbilWarnaListener {</pre><pre><strong>  override fun </strong>onCancel(dialog: AmbilWarnaDialog) {</pre><pre> }</pre><pre><strong>  override fun </strong>onOk(dialog: AmbilWarnaDialog ,color: Int) {</pre><pre><strong>   chosenColor </strong>= color</pre><pre>  }</pre><pre> })</pre><pre> colorPicker.show()</pre><pre>}</pre><pre><strong>private fun </strong>chooseTexture() {</pre><pre><strong> texture </strong>= <strong>true</strong></pre><pre>}</pre><pre><strong>fun </strong>getResizedBitmap(bm: Bitmap, newWidth: Int, newHeight: Int): Bitmap {</pre><pre><strong> val </strong>width = bm.getWidth()</pre><pre><strong> val </strong>height = bm.getHeight()</pre><pre><strong> val </strong>scaleWidth = newWidth / width.toFloat()</pre><pre><strong> val </strong>scaleHeight = newHeight / height.toFloat()</pre><pre><em> // CREATE A MATRIX FOR THE MANIPULATION</em></pre><pre><strong> val </strong>matrix = Matrix()</pre><pre><em> // RESIZE THE BIT MAP</em></pre><pre> matrix.postScale(scaleWidth, scaleHeight)</pre><pre><em> // “RECREATE” THE NEW BITMAP</em></pre><pre><strong> val </strong>resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,  matrix, <strong>true</strong>)</pre><pre><strong> return </strong>resizedBitmap</pre><pre>}</pre><pre><strong>private fun </strong>getTextureImage(): Mat {</pre><pre><strong> var </strong>textureImage = BitmapFactory.decodeResource(getResources(), R.drawable.<em>texture_small_brick_red</em>)</pre><pre> textureImage = getResizedBitmap(textureImage,<strong>bitmap</strong>.<em>width</em>,<strong>bitmap</strong>.<em>height</em>)</pre><pre><strong> val </strong>texture = Mat()</pre><pre> Utils.bitmapToMat(textureImage,texture)</pre><pre> Imgproc.cvtColor(texture,texture,Imgproc.<em>COLOR_RGBA2RGB</em>)</pre><pre><strong> return </strong>texture</pre><pre>}</pre><h3>Step 2: Opening Camera and Gallery</h3><p>Add the functions to open gallery and camera, copy and paste the below functions to do that.</p><pre><strong>private fun </strong>openCamera() {</pre><pre><strong> if </strong>(ActivityCompat.checkSelfPermission(<em>baseContext</em>, Manifest.permission.<em>CAMERA</em>)</pre><pre>!= PackageManager.<em>PERMISSION_GRANTED</em>) {</pre><pre>  ActivityCompat.requestPermissions(<strong>this</strong>, <strong>PERMISSIONS</strong>, LoadImage.<strong>PICK_FROM_CAMERA</strong>.<strong>ordinal</strong>)</pre><pre>  } <strong>else </strong>{</pre><pre><strong>  val </strong>captureIntent = Intent(MediaStore.<em>ACTION_IMAGE_CAPTURE</em>)</pre><pre><strong>  var </strong>photoFile: File? = <strong>null</strong></pre><pre><strong>  try </strong>{</pre><pre>   photoFile = createImageFile()</pre><pre>  } <strong>catch </strong>(ex: IOException) {</pre><pre><em>   // Error occurred while creating the File</em></pre><pre>  }</pre><pre><strong>  if </strong>(photoFile != <strong>null</strong>) {</pre><pre><strong>   val </strong>photoURI =   FileProvider.getUriForFile(<strong>this</strong>,<strong>”com.example.paintapp.provider”</strong>, photoFile)</pre><pre>   captureIntent.putExtra(MediaStore.<em>EXTRA_OUTPUT</em>, photoURI)</pre><pre>   startActivityForResult(captureIntent,   LoadImage.<strong>PICK_FROM_CAMERA</strong>.<strong>ordinal</strong>)</pre><pre>  }</pre><pre> }</pre><pre>}</pre><pre><strong>private fun </strong>openGallery() {</pre><pre><strong> val </strong>i = Intent(Intent.<em>ACTION_PICK</em>,MediaStore.Images.Media.<em>EXTERNAL_CONTENT_URI</em>)</pre><pre> startActivityForResult(i, LoadImage.<strong>PICK_FROM_GALLERY</strong>.<strong>ordinal</strong>)</pre><pre>}</pre><h3>Step 3: Creating and Saving Image files</h3><p>Now, add createImageFile() function that’ll create an image file for you and saveImage() that’ll save the image in that file. Copy and paste the code below:</p><pre><strong>private fun </strong>createImageFile(): File {</pre><pre><strong> val </strong>timeStamp = SimpleDateFormat(<strong>“yyyyMMdd_HHmmss”</strong>, Locale.getDefault()).format( Date())</pre><pre><strong> val </strong>imageFileName = <strong>“IMG_” </strong>+ timeStamp + <strong>“_”</strong></pre><pre><strong> val </strong>storageDir = getExternalFilesDir(Environment.<em>DIRECTORY_PICTURES</em>)</pre><pre><strong> val </strong>image = File.createTempFile(</pre><pre>  imageFileName, <em>/* prefix */</em></pre><pre><strong>  “.jpg”</strong>, <em>/* suffix */</em></pre><pre>  storageDir <em>/* directory */</em></pre><pre> )</pre><pre><strong> imageFilePath </strong>= image.getAbsolutePath()</pre><pre><strong> return </strong>image;</pre><pre>}</pre><pre><strong>private fun </strong>saveImage(image: Bitmap) {</pre><pre><strong> val </strong>pictureFile = createImageFile()</pre><pre><strong> if </strong>(pictureFile == <strong>null</strong>) {</pre><pre>  Log.e(<strong>TAG</strong>, <strong>“Error creating media file, check storage permissions: “</strong>)</pre><pre><strong>  return</strong></pre><pre> }</pre><pre><strong> try </strong>{</pre><pre><strong>  val </strong>fos = FileOutputStream(pictureFile);</pre><pre>  image.compress(Bitmap.CompressFormat.<strong>PNG</strong>, 90, fos)</pre><pre>  fos.close()</pre><pre> } <strong>catch </strong>(e: FileNotFoundException) {</pre><pre>  Log.e(<strong>TAG</strong>, <strong>“File not found: “ </strong>+ e.<strong>message</strong>)</pre><pre> } <strong>catch </strong>(e: IOException) {</pre><pre>  Log.e(<strong>TAG</strong>, <strong>“Error accessing file: “ </strong>+ e.<strong>message</strong>)</pre><pre> }</pre><pre>}</pre><h3>Step 4: Loading Image from Gallery</h3><p>Add the below function to load the image from gallery:</p><pre><strong>private fun </strong>loadFromGallery(data:Intent?) {</pre><pre><strong> val </strong>selectedImage = data?.<em>data</em></pre><pre><strong> val </strong>filePathColumn: Array&lt;String&gt; = <em>arrayOf</em>(MediaStore.Images.Media.<em>DATA</em>)</pre><pre><strong> val </strong>cursor = getContentResolver().query(selectedImage!!,filePathColumn, <strong>null</strong>, <strong>null</strong>, <strong>null</strong>)</pre><pre> cursor?.moveToFirst()</pre><pre><strong> val </strong>columnIndex = cursor?.getColumnIndex(filePathColumn[0])</pre><pre><strong> val </strong>picturePath = cursor?.getString(columnIndex!!)</pre><pre> cursor?.close()</pre><pre><strong> bitmap </strong>= BitmapFactory.decodeFile(picturePath)</pre><pre><strong> bitmap </strong>= getResizedBitmap(<strong>bitmap</strong>,<strong>bitmap</strong>.<em>width</em>/5,<strong>bitmap</strong>.<em>height</em>/5)</pre><pre> showImage()</pre><pre>}</pre><h3>Painting the wall</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FLargeHorribleHapuku&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fthumbs.gfycat.com%2FLargeHorribleHapuku-mobile.mp4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="480" height="368" frameborder="0" scrolling="no"><a href="https://medium.com/media/125fc2f71ac70925c34d12234fe61b10/href">https://medium.com/media/125fc2f71ac70925c34d12234fe61b10/href</a></iframe><h3>Apply Paint</h3><p>Painting the wall consist of the following steps:</p><ol><li>Get the Greyscale and HSV matrix for the given image</li><li>Get the S-channel matrix from the HSV image</li><li>Blur the image matrices</li><li>Do Canny Edge detection on both of them</li><li>Now, merge (linear/alpha blend) both the canny edge detected images</li><li>Dilate the resulting image matrix</li><li>Flood-fill your original RGB image by taking the above resulting image as a mask and “tl(variable that stores the touch coordinates)” as your seed point with your chosen colour</li><li>Now, again take the HSV matrix of your original image and merge the V-channel matrix into this flood-filled image</li><li>Finally add merge the resulting image with your original image.</li><li>Show your image to the user</li></ol><p>The below function will do the task for you:</p><pre><strong>private fun </strong>rpPaintHSV(bitmap: Bitmap, p: Point): Mat {</pre><pre><strong> val </strong>cannyMinThres = 30.0</pre><pre><strong> val </strong>ratio = 2.5</pre><pre><em> // show intermediate step results</em></pre><pre><em> // grid created here to do that</em></pre><pre> showResultLayouts()</pre><pre><strong> val </strong>mRgbMat = Mat()</pre><pre> Utils.bitmapToMat(bitmap, mRgbMat)</pre><pre> showImage(mRgbMat,inputImage)</pre><pre> Imgproc.cvtColor(mRgbMat,mRgbMat,Imgproc.<em>COLOR_RGBA2RGB</em>)</pre><pre><strong> val </strong>mask = Mat(Size(mRgbMat.width()/8.0, mRgbMat.height()/8.0), CvType.<em>CV_8UC1</em>, Scalar(0.0))</pre><pre><em> // Imgproc.dilate(mRgbMat, mRgbMat,mask, Point(0.0,0.0), 5)</em></pre><pre><strong> val </strong>img = Mat()</pre><pre> mRgbMat.copyTo(img)</pre><pre><em> // grayscale</em></pre><pre><strong> val </strong>mGreyScaleMat = Mat()</pre><pre> Imgproc.cvtColor(mRgbMat, mGreyScaleMat, Imgproc.<em>COLOR_RGB2GRAY</em>, 3)</pre><pre> Imgproc.medianBlur(mGreyScaleMat,mGreyScaleMat,3)</pre><pre><strong> val </strong>cannyGreyMat = Mat()</pre><pre> Imgproc.Canny(mGreyScaleMat, cannyGreyMat, cannyMinThres, cannyMinThres*ratio, 3)</pre><pre> showImage(cannyGreyMat,greyScaleImage)</pre><pre><em> //hsv</em></pre><pre><strong> val </strong>hsvImage = Mat()</pre><pre> Imgproc.cvtColor(img,hsvImage,Imgproc.<em>COLOR_RGB2HSV</em>)</pre><pre><em> //got the hsv values</em></pre><pre><strong> val </strong>list = ArrayList&lt;Mat&gt;(3)</pre><pre> Core.split(hsvImage, list)</pre><pre><strong> val </strong>sChannelMat = Mat()</pre><pre> Core.merge(<em>listOf</em>(list.get(1)), sChannelMat)</pre><pre> Imgproc.medianBlur(sChannelMat,sChannelMat,3)</pre><pre> showImage(sChannelMat,floodFillImage)</pre><pre><em> // canny</em></pre><pre><strong> val </strong>cannyMat = Mat()</pre><pre> Imgproc.Canny(sChannelMat, cannyMat, cannyMinThres, cannyMinThres*ratio, 3)</pre><pre> showImage(cannyMat,HSVImage)</pre><pre> Core.addWeighted(cannyMat,0.5, cannyGreyMat,0.5 ,0.0,cannyMat)</pre><pre> Imgproc.dilate(cannyMat, cannyMat,mask, Point(0.0,0.0), 5)</pre><pre> showImage(cannyMat,cannyEdgeImage)</pre><pre><strong> val </strong>displayMetrics = DisplayMetrics()</pre><pre><em> windowManager</em>.<em>defaultDisplay</em>.getMetrics(displayMetrics)</pre><pre><strong> val </strong>height = displayMetrics.<strong>heightPixels</strong></pre><pre><strong> val </strong>width = displayMetrics.<strong>widthPixels</strong></pre><pre><strong> val </strong>seedPoint = Point(p.<strong>x</strong>*(mRgbMat.width()/width.toDouble()), p.<strong>y</strong>*(mRgbMat.height()/height.toDouble()))</pre><pre>// Make sure to resize the cannyMat or it&#39;ll throw an error</pre><pre> Imgproc.resize(cannyMat, cannyMat, Size(cannyMat.width() + 2.0, cannyMat.height() + 2.0))</pre><pre> Imgproc.medianBlur(mRgbMat,mRgbMat,15)</pre><pre><strong> val </strong>floodFillFlag = 8</pre><pre> Imgproc.floodFill(</pre><pre>  mRgbMat,</pre><pre>  cannyMat,</pre><pre>  seedPoint,</pre><pre>  Scalar(Color.red(<strong>chosenColor</strong>).toDouble(),Color.green(<strong>chosenColor</strong>).toDouble(),Color.blue(<strong>chosenColor</strong>).toDouble()),</pre><pre>  Rect(),</pre><pre>  Scalar(5.0, 5.0, 5.0),</pre><pre>  Scalar(5.0, 5.0, 5.0),</pre><pre>  floodFillFlag</pre><pre> )</pre><pre><em> // showImage(mRgbMat,floodFillImage)</em></pre><pre> Imgproc.dilate(mRgbMat, mRgbMat, mask, Point(0.0,0.0), 5)</pre><pre><em> //got the hsv of the mask image</em></pre><pre><strong> val </strong>rgbHsvImage = Mat()</pre><pre> Imgproc.cvtColor(mRgbMat,rgbHsvImage,Imgproc.<em>COLOR_RGB2HSV</em>)</pre><pre><strong> val </strong>list1 = ArrayList&lt;Mat&gt;(3)</pre><pre> Core.split(rgbHsvImage, list1)</pre><pre><em> //merged the “v” of original image with mRgb mat</em></pre><pre><strong> val </strong>result = Mat()</pre><pre> Core.merge(<em>listOf</em>(list1.get(0),list1.get(1),list.get(2)), result)</pre><pre><em> // converted to rgb</em></pre><pre> Imgproc.cvtColor(result, result, Imgproc.<em>COLOR_HSV2RGB</em>)</pre><pre> Core.addWeighted(result,0.7, img,0.3 ,0.0,result )</pre><pre> showImage(result,outputImage)</pre><pre><strong> return </strong>result</pre><pre>}</pre><h3>Apply Texture</h3><p>Applying texture is similar to applying paint to your image:</p><ol><li>Follow Steps 1 to 6 (given above) for the original image</li><li>For the flood-fill part:</li></ol><p>a) wall-image: darken the area of the wall by taking the canny edge detected image as the mask.</p><p>b) texture image: brighten the area of the wall by taking the canny edge detected image as the mask.</p><p>3. Merge the two image matrices by using Bitwise_or operator on the two matrices.</p><p>4. Finally add the V-Channel of the original image and show it to the user.</p><p>Copy and paste the below method:</p><pre><strong>private fun </strong>applyTexture(bitmap: Bitmap, p: Point) {</pre><pre><strong> val </strong>cannyMinThres = 30.0</pre><pre><strong> val </strong>ratio = 2.5</pre><pre><em> // show intermediate step results</em></pre><pre><em> // grid created here to do that</em></pre><pre> showResultLayouts()</pre><pre><strong> val </strong>mRgbMat = Mat()</pre><pre> Utils.bitmapToMat(bitmap, mRgbMat)</pre><pre> showImage(mRgbMat,inputImage)</pre><pre> Imgproc.cvtColor(mRgbMat,mRgbMat,Imgproc.<em>COLOR_RGBA2RGB</em>)</pre><pre><strong> val </strong>mask = Mat(Size(mRgbMat.width()/8.0, mRgbMat.height()/8.0), CvType.<em>CV_8UC1</em>, Scalar(0.0))</pre><pre><em> // Imgproc.dilate(mRgbMat, mRgbMat,mask, Point(0.0,0.0), 5) </em></pre><pre><strong> val </strong>img = Mat()</pre><pre> mRgbMat.copyTo(img)</pre><pre><em> // grayscale</em></pre><pre><strong> val </strong>mGreyScaleMat = Mat()</pre><pre> Imgproc.cvtColor(mRgbMat, mGreyScaleMat, Imgproc.<em>COLOR_RGB2GRAY</em>, 3)</pre><pre> Imgproc.medianBlur(mGreyScaleMat,mGreyScaleMat,3)</pre><pre><strong> val </strong>cannyGreyMat = Mat()</pre><pre> Imgproc.Canny(mGreyScaleMat, cannyGreyMat, cannyMinThres, cannyMinThres*ratio, 3)</pre><pre> showImage(cannyGreyMat,greyScaleImage)</pre><pre><em> //hsv</em></pre><pre><strong> val </strong>hsvImage = Mat()</pre><pre> Imgproc.cvtColor(img,hsvImage,Imgproc.<em>COLOR_RGB2HSV</em>)</pre><pre><em> //got the hsv values</em></pre><pre><strong> val </strong>list = ArrayList&lt;Mat&gt;(3)</pre><pre> Core.split(hsvImage, list)</pre><pre><strong> val </strong>sChannelMat = Mat()</pre><pre> Core.merge(<em>listOf</em>(list.get(1)), sChannelMat)</pre><pre> Imgproc.medianBlur(sChannelMat,sChannelMat,3)</pre><pre> showImage(sChannelMat,floodFillImage)</pre><pre><em> // canny</em></pre><pre><strong> val </strong>cannyMat = Mat()</pre><pre> Imgproc.Canny(sChannelMat, cannyMat, cannyMinThres, cannyMinThres*ratio, 3)</pre><pre> showImage(cannyMat,HSVImage)</pre><pre> Core.addWeighted(cannyMat,0.5, cannyGreyMat,0.5 ,0.0,cannyMat)</pre><pre> Imgproc.dilate(cannyMat, cannyMat,mask, Point(0.0,0.0), 5)</pre><pre><strong> val </strong>displayMetrics = DisplayMetrics()</pre><pre><em> windowManager</em>.<em>defaultDisplay</em>.getMetrics(displayMetrics)</pre><pre><strong> val </strong>height = displayMetrics.<strong>heightPixels</strong></pre><pre><strong> val </strong>width = displayMetrics.<strong>widthPixels</strong></pre><pre><strong> val </strong>seedPoint = Point(p.<strong>x</strong>*(mRgbMat.width()/width.toDouble()), p.<strong>y</strong>*(mRgbMat.height()/height.toDouble()))</pre><pre>// Make sure to resize the cannyMat or it&#39;ll throw an error</pre><pre> Imgproc.resize(cannyMat, cannyMat, Size(cannyMat.width() + 2.0, cannyMat.height() + 2.0))</pre><pre><strong> val </strong>cannyMat1 = Mat()</pre><pre> cannyMat.copyTo(cannyMat1)</pre><pre><strong> val </strong>wallMask = Mat(mRgbMat.size(),mRgbMat.type())</pre><pre><strong> val </strong>floodFillFlag = 8</pre><pre> Imgproc.floodFill(</pre><pre>  wallMask,</pre><pre>  cannyMat,</pre><pre>  seedPoint,</pre><pre>  Scalar(255.0,255.0,255.0),</pre><pre>  Rect(),</pre><pre>  Scalar(5.0, 5.0, 5.0),</pre><pre>  Scalar(5.0, 5.0, 5.0),</pre><pre>  floodFillFlag</pre><pre> ) </pre><pre> showImage(wallMask,greyScaleImage)</pre><pre> showImage(cannyMat,cannyEdgeImage)</pre><pre> Imgproc.floodFill(</pre><pre>  mRgbMat,</pre><pre>  cannyMat1,</pre><pre>  seedPoint,</pre><pre>  Scalar(0.0,0.0,0.0),</pre><pre>  Rect(),</pre><pre>  Scalar(5.0, 5.0, 5.0),</pre><pre>  Scalar(5.0, 5.0, 5.0),</pre><pre>  floodFillFlag</pre><pre> )</pre><pre> showImage(mRgbMat,HSVImage)</pre><pre><strong> val </strong>texture = getTextureImage() </pre><pre><strong> val </strong>textureImgMat = Mat()</pre><pre> Core.bitwise_and(wallMask ,texture,textureImgMat)</pre><pre> showImage(textureImgMat,floodFillImage)</pre><pre><strong> val </strong>resultImage = Mat()</pre><pre> Core.bitwise_or(textureImgMat,mRgbMat,resultImage)</pre><pre> showImage(resultImage,outputImage)</pre><pre><em> ////alpha blending</em></pre><pre><em> //got the hsv of the mask image</em></pre><pre><strong> val </strong>rgbHsvImage = Mat()</pre><pre> Imgproc.cvtColor(resultImage,rgbHsvImage,Imgproc.<em>COLOR_RGB2HSV</em>)</pre><pre><strong> val </strong>list1 = ArrayList&lt;Mat&gt;(3)</pre><pre> Core.split(rgbHsvImage, list1)</pre><pre><em> //merged the “v” of original image with mRgb mat</em></pre><pre><strong> val </strong>result = Mat()</pre><pre> Core.merge(<em>listOf</em>(list1.get(0),list1.get(1),list.get(2)), result)</pre><pre><em> // converted to rgb </em></pre><pre> Imgproc.cvtColor(result, result, Imgproc.<em>COLOR_HSV2RGB</em>) </pre><pre> Core.addWeighted(result,0.8, img,0.2 ,0.0,result )</pre><pre> showImage(result,outputImage)</pre><pre>}</pre><h3>Final result</h3><p>Now, after getting the image and tapping on the wall inside the image the wall should be painted red (red is the default colour, of course you can choose other colours and your texture from the dropdown menu).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*MajhmaPn3vbNQPImEWH79w.png" /></figure><h3>Goodbye</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o72FcJmLzIdYJdmDe%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Famericasgottalent-thumbs-up-agt-simon-cowell-3o72FcJmLzIdYJdmDe&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExenc0ZGF2NTJidm5jeWRvbXViZnZkeHBhMmliOGE1Y3UzZGNpMG56ciZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2F3o72FcJmLzIdYJdmDe%2Fgiphy_s.gif&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/88ec4ae2bb6002589c6c056ba2168716/href">https://medium.com/media/88ec4ae2bb6002589c6c056ba2168716/href</a></iframe><p>Congratulations!!! , you painted the wall inside the image using OpenCV libraries.</p><p>And with that my friend, you are done. Great job!</p><p>You can find the whole code for this app here : <a href="https://github.com/tushar40/Wall-Paint-App-Android">https://github.com/tushar40/Wall-Paint-App-Android</a></p><p>I hope you enjoyed this tutorial. Feel free to post your questions and comments on the forums, I can’t wait for what you guys come up with for your next great app! ;)</p><p>This is a post by Tushar Gusain, an Android and iOS developer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abac3a79c790" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using the new Instagram Basic Display API in Xcode using UIStoryboard:]]></title>
            <link>https://tushargusain40.medium.com/using-the-new-instagram-basic-display-api-using-swift-5-ios-10e94292e280?source=rss-246d3cfcb899------2</link>
            <guid isPermaLink="false">https://medium.com/p/10e94292e280</guid>
            <category><![CDATA[uistoryboard]]></category>
            <category><![CDATA[instagram]]></category>
            <category><![CDATA[swift-5]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[facebook-graph-api]]></category>
            <dc:creator><![CDATA[Tushar Gusain]]></dc:creator>
            <pubDate>Sat, 14 Mar 2020 09:20:33 GMT</pubDate>
            <atom:updated>2020-05-30T12:07:05.417Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn about URLSession, using the new Instagram Basic Display API, and UIStoryboard in this Swift 5 iOS Tutorial.</h4><h3>Version:-</h3><p>Swift 5, iOS 13, Xcode 11</p><p>This is a post by Tushar Gusain, an Android/iOS developer.</p><p>Social networks are an indispensable part of our daily lives. Not only do we access social networks via websites like twitter.com, facebook.com or instagram.com, but we also find social features in apps, websites, blogs, video games, and more.</p><p>Integrating some social features into your apps can increase the virality of your app, they can help you identify and retain customers, and can add value to your app.</p><p>Before the <strong>update </strong>starting October 15, 2019, <a href="https://api.instagram.com/v1">https://api.instagram.com/v1</a> was used to get the Instagram User Access Token, Permissions and Data from an Instagram user. After that the api was moved and is now owned by Facebook. You can find more information about the deprecation here: <a href="https://www.instagram.com/developer/">https://www.instagram.com/developer/</a> .</p><p>Keep reading to know how simple it is to integrate the new Instagram API in your app.</p><h3>About the updated Instagram Api</h3><p>Facebook provides two apis to integrate into your app to get the instagram features inside your app.The first one is Instagram <a href="https://developers.facebook.com/docs/instagram-api">Graph API</a> ,which can allow the users of our app to access data in their Instagram Business and Instagram Creator accounts. It is categorized under the Business Apis provided by Facebook.</p><p>The second one (which we will be using in this tutorial) is the Instagram <a href="https://developers.facebook.com/docs/instagram-basic-display-api">Basic Display API</a>. It is categorized under the Consumer Api provided by Facebook , it’ll allow the users of our app to get basic profile information, photos, and videos from their Instagram accounts.This API is intended for non-Business and non-Creator Instagram users.</p><p>As we will be using a non-Business and non-Creator account in this tutorial, therefore we are using the Instagram Basic Display Api.</p><p>For more information on the Instagram Apis follow this link: <a href="https://developers.facebook.com/docs/instagram">https://developers.facebook.com/docs/instagram</a></p><h3>Getting started</h3><p>Before we start you should have your own Facebook developer account , you can create one here : <a href="https://developers.facebook.com/apps">Facebook Developer Account</a> .</p><h3>Step 1: Create a Facebook App</h3><p>Go to <a href="https://developers.facebook.com/">developers.facebook.com</a>, click <strong>My Apps</strong>, and create a new app. Once you have created the app and are in the App Dashboard, navigate to <strong>Settings</strong> <strong>&gt;</strong> <strong>Basic</strong>, scroll to the bottom of page, and click Add Platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/0*v5OdiMaWNQPeAB0w" /></figure><p>Choose iOS, add your iOS Bundle ID and Shared Secret, and save your changes.</p><p>To get your Bundle ID click on your project in Xcode, choose your <strong>Target</strong>, then click on <strong>General</strong>, you’ll find your Bundle ID inside <strong>Identity</strong> <strong>&gt;</strong> <strong>Display Name.</strong></p><p>For your Shared Secret copy your <strong>App Secret</strong> and paste it inside Shared Secret.</p><h3>Step 2: Configure Instagram Basic Display</h3><p>Click <strong>Products</strong>, locate the Instagram product, and click <strong>Set Up</strong> to add it to your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/0*KjmEN1GXAfuUR_VI" /></figure><p>Click <strong>Basic Display</strong>, scroll to the bottom of the page, then click <strong>Create New App</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/0*KGhGTarBiENfuNT0" /></figure><p>Now, inside Display Name field, enter the name of the Facebook app you just created.</p><p>Next in Valid OAuth Redirect URIs, enter your website’s URL or any other website’s URL. Normally this would be a dedicated URI that can capture redirect query string parameters, but for this tutorial you can use any website’s URL.</p><p>For example: <a href="https://www.google.com/">https://www.google.com/</a></p><p>Copy the complete URL somewhere since you will need it in later steps to get authorization codes and access tokens.</p><p>In Deauthorize Callback URL, enter the copied URL again. Eventually you will have to change this to a URL that can handle deauthorization notifications, but for the purposes of this tutorial, you can re-use the above copied URL.</p><p>Do the same forData Deletion Request Callback URL.</p><p>Skip the App Review section for now since you will not be switching the app to Live Mode during the tutorial.</p><h3>Step 3: Add an Instagram Test User</h3><p>Navigate to <strong>Roles</strong> <strong>&gt;</strong> <strong>Roles</strong> and scroll down to the <strong>Instagram Testers</strong> section. Click <strong>Add Instagram Testers</strong> and enter your Instagram account’s username and send the invitation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/0*0X1qHPDJ5jy2TYVO" /></figure><p>Open a new web browser and go to <a href="http://www.instagram.com/">www.instagram.com</a> and sign into your Instagram account that you just invited. Navigate to <strong>(Profile Icon) &gt; Edit Profile &gt; Apps and Websites &gt; Tester Invites</strong> and accept the invitation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/0*0MfuRkWlKAUaZb9p" /></figure><p>Note:- Do not add any profile in the <strong>Testers </strong>section, as we are not testing the facebook app here.</p><p>Your Instagram account is now eligible to be accessed by your Facebook app while it is in Development Mode.</p><h3>Setting up your Xcode project</h3><h3>Step 1: Create an Xcode project</h3><p>Open <strong>Xcode</strong>, click on <strong>New project</strong>, then select <strong>Single View App</strong> and click <strong>Next</strong>.</p><p>Now, Enter the name of your App in the <strong>Product Name</strong> field, then, select <strong>Swift </strong>as Language and <strong>Storyboard </strong>as User Interface.</p><p>After that click on <strong>Next</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*9iznyg1PT5KJ_-XKxgRlUA.png" /></figure><h3>Step 2: Create the InstagramApi class</h3><p>Now, create a new <strong>Swift file</strong>, name it <strong>InstagramApi</strong>, then inside the file declare a <strong>class InstagramApi</strong>, this class will be responsible for handling api requests. Getting the oauth token, fetching your feed, media etc.</p><pre>class InstagramApi {</pre><pre>  static let shared = InstagramApi()</pre><pre>  private let instagramAppID = “{your-instagram-app-id}”</pre><pre>  private let redirectURIURLEncoded = “https%3A%2F%2Fwww.hotcocoasoftware.com%2F&quot;</pre><pre>  private let redirectURI = “https://www.hotcocoasoftware.com/&quot;</pre><pre>  private let app_secret = “{your-app-secret}”</pre><pre>  private let boundary = “boundary=\(NSUUID().uuidString)”</pre><pre>  private init () {}</pre><pre>}</pre><p>Now, copy and paste the above code inside your InstagramApi class.</p><p>Replace {your-instagram-app-id} with the instagram app id in your facebook app.</p><p>(Note:- Do not confuse instagram app id with facebook app id, your instagram app id is given inside <strong>Products &gt; Instagram&gt; Basic Display &gt; Instagram App ID</strong> )</p><p>Then , replace {your-app-secret} with your Instagram app secret.</p><p>Here, I have used <a href="https://www.hotcocoasoftware.com/">https://www.hotcocoasoftware.com/</a> as the redirect URI , you can use any URI you want for this tutorial.</p><p>For example, if you use <a href="https://www.google.com/">https://www.google.com/</a> as redirectURI your then change the redirectURIURLEncoded’s value to “https%3A%2F%2Fwww.google.com%2F&quot;.</p><p>Now, declare the below enums inside InstagramApi class :</p><pre>private enum BaseURL: String {</pre><pre>  case displayApi = “https://api.instagram.com/&quot;</pre><pre>  case graphApi = “https://graph.instagram.com/&quot;</pre><pre>}</pre><pre>private enum Method: String {</pre><pre>  case authorize = “oauth/authorize”</pre><pre>  case access_token = “oauth/access_token”</pre><pre>}</pre><h3>Step 3: Create a model file</h3><p>Now, create a new <strong>swift file</strong> , name it <strong>InstagramResponse. </strong>Inside the file declare the below structs<strong>:</strong></p><pre>struct InstagramTestUser: Codable {</pre><pre>  var access_token: String</pre><pre>  var user_id: Int</pre><pre>}</pre><pre>struct InstagramUser: Codable {</pre><pre>  var id: String</pre><pre>  var username: String</pre><pre>}</pre><pre>struct Feed: Codable {</pre><pre>  var data: [MediaData]</pre><pre>  var paging: PagingData</pre><pre>}</pre><pre>struct MediaData: Codable {</pre><pre>  var id: String</pre><pre>  var caption: String?</pre><pre>}</pre><pre>struct PagingData: Codable {</pre><pre>  var cursors: CursorData  </pre><pre>  var next: String</pre><pre>}</pre><pre>struct CursorData: Codable {</pre><pre>  var before: String</pre><pre>  var after: String</pre><pre>}</pre><pre>struct InstagramMedia: Codable {</pre><pre>  var id: String</pre><pre>  var media_type: MediaType</pre><pre>  var media_url: String</pre><pre>  var username: String</pre><pre>  var timestamp: String</pre><pre>}</pre><pre>enum MediaType: String,Codable {</pre><pre>  case IMAGE</pre><pre>  case VIDEO</pre><pre>  case CAROUSEL_ALBUM</pre><pre>}</pre><h3>Step 4: Setting up the Viewcontroller in Storyboard</h3><p>(If you want to use the SwiftUI instead of UIStoryboard you can follow this post :- <a href="https://medium.com/@tushargusain40/using-the-new-instagram-basic-display-api-using-swift-5-ios-bf06d93c4d8b?sk=b3f8d481076cc0ceb9e3c6161704b01e">https://medium.com/@tushargusain40/using-the-new-instagram-basic-display-api-using-swift-5-ios-bf06d93c4d8b?sk=b3f8d481076cc0ceb9e3c6161704b01e</a> )</p><p>Add an imageView to your ViewController inside your<strong> Main.storyboard</strong> file and set it’s <strong>Content Mode</strong> to “Aspect fill<strong>”</strong>.</p><p>Constraint it as big as the superview, and above this Imageview add two buttons as shown in the preview below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RTq90x40xnA5V6Dd" /></figure><p>One button will be an “Instagram icon” and the other will be a “Fetch Image to background” text.</p><p>Now, go to your <strong>ViewController.swift</strong> file and add the following lines of code:-</p><pre>var instagramApi = InstagramApi.shared</pre><pre>var testUserData = InstagramTestUser(access_token: “”, user_id: 0)</pre><pre>var instagramUser: InstagramUser?</pre><pre>var signedIn = false</pre><pre>@IBOutlet var backgroundImageView: UIImageView!</pre><pre>@IBAction func authenticateOrSignIn(_ sender: UIButton) { }</pre><pre>@IBAction func fetchImageToBackground(_ sender: UIButton) { }</pre><p>Connect the backgroundImageView to the background image of the ViewController, authenticateOrSignIn with the “Instagram icon” Button and lastly, fetchImageToBackground to the “Fetch Image to Background” text Button.</p><p>On running the App you’ll get the following screen…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*kEhp497mvpFfdjmh" /></figure><p>Now , you are all set to call the Instagram Api.</p><h3>Getting your Access Token</h3><h3>Get Authorization</h3><p>We need to authenticate the Test User, to do this we have to call the below url :</p><pre><a href="https://api.instagram.com/oauth/authorize">https://api.instagram.com/oauth/authorize</a></pre><pre>?app_id={app-id}</pre><pre>&amp;redirect_uri={redirect-uri-urlencoded}</pre><pre>&amp;scope=user_profile,user_media</pre><pre>&amp;response_type=code</pre><p>This will provide you with an authorization window, showing you the test user id that you added in the instagram app.</p><p>Go inside your <strong>InstagramApi</strong> class, create a function <strong>authorizeApp</strong> and paste the following code:</p><pre>func authorizeApp(completion: @escaping (_ url: URL?) -&gt; Void ) {</pre><pre>  let urlString = “\(BaseURL.displayApi.rawValue)\(Method.authorize.rawValue)?app_id=\(instagramAppID)&amp;redirect_uri=\(redirectURIURLEncoded)&amp;scope=user_profile,user_media&amp;response_type=code”</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>    if let response = response {</pre><pre>      print(response)</pre><pre>      completion(response.url)</pre><pre>    }</pre><pre>  })</pre><pre>  task.resume()</pre><pre>}</pre><p>This function will call the above URL for authorization.</p><p>Now, go to the ViewController.swift class and add the following variable :</p><pre>var testUserData = InstagramTestUser(access_token: “”, user_id: 0)</pre><p>After this create a new <strong>Swift File</strong>, name it <strong>WebViewController. </strong>Then, add the following variables to the class:</p><pre>import WebKit</pre><pre>class WebViewController: UIViewController, WKNavigationDelegate {</pre><pre>  var instagramApi: InstagramApi?</pre><pre>  var testUserData: InstagramTestUser?</pre><pre>  var mainVC: ViewController?</pre><pre>  @IBOutlet var webView: WKWebView! {</pre><pre>    didSet {</pre><pre>      webView.navigationDelegate = self</pre><pre>    }</pre><pre>  }</pre><pre>}</pre><p>Now, create a new ViewController in <strong>Main.storyboard</strong> file, add a Webkit View to it.Your Storyboard will look like this preview:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*daAJBfEVzdxTQfIG" /></figure><p>Select the class as <strong>WebViewController </strong>and write <strong>WebView </strong>inside<strong> </strong>Storyboard ID parameter.</p><p>After that connect the @IBOutlet var webView: WKWebView!!to the WebkitView inside the new ViewController.</p><p>Now, inside your WebViewController add the following three methods:</p><pre>override func viewDidLoad() {</pre><pre>  super.viewDidLoad()</pre><pre>  //Authorize the user</pre><pre>}</pre><pre>func webView(<strong>_</strong> webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {</pre><pre>  //Get user id and token  </pre><pre>  decisionHandler(WKNavigationActionPolicy.allow)</pre><pre>}</pre><pre>func dismissViewController() {</pre><pre>  self.dismiss(animated: true) {</pre><pre>    self.mainVC?.testUserData = self.testUserData!</pre><pre>  }</pre><pre>}</pre><p>Then, inside your viewDidLoad method add the following lines of code:</p><pre>override func viewDidLoad() {</pre><pre>  super.viewDidLoad()</pre><pre>  instagramApi?.authorizeApp { (url) in</pre><pre>    DispatchQueue.main.async {</pre><pre>     self.webView.load(URLRequest(url: url!))</pre><pre>    }</pre><pre>  }</pre><pre>}</pre><p>Now, go inside your ViewController.swift file, add the following code to authenticateOrSignIn method:</p><pre>@IBAction func authenticateOrSignIn(<strong>_</strong> sender: UIButton) {</pre><pre>  if self.testUserData.user_id == 0 {</pre><pre>    presentWebViewController()</pre><pre>  } else {</pre><pre>  //get account user’s details</pre><pre>}</pre><p>After this add the following method to your ViewController class:</p><pre>func presentWebViewController() {</pre><pre>  let storyBoard : UIStoryboard = UIStoryboard(name: “Main”, bundle:nil)</pre><pre>  let webVC = storyBoard.instantiateViewController(withIdentifier: “WebView”) as! WebViewController</pre><pre>  webVC.instagramApi = self.instagramApi</pre><pre>  webVC.mainVC = self</pre><pre>  self.present(webVC, animated:true)</pre><pre>}</pre><p>Now, run the app , clicking the instagram icon will present one of the following sheet as shown in the images below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*nw_dWB5og-jQ8IhC" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*_V5q-6ZmUru4N-pF" /></figure><p>Enter your username &amp; password(or click on the continue button if you got the second screen) to authorize the test user and grant your app access to your profile data. Upon success, the page will redirect you to the redirect URI you included in the previous step and append an Authorization Code. For example:</p><pre><a href="https://hotcocoasoftware.com/auth/?code=AQDp3TtBQQ...#_">https://hotcocoasoftware.com/auth/?code=AQDp3TtBQQ...#_</a></pre><p>Note:- #_ has been appended to the end of the redirect URI, but it is not part of the code itself.</p><h3>Exchange the Code For a Token</h3><p>Inside your <strong>InstagramApi</strong> class create a new function <strong>getTokenFromCallbackURL</strong>, and add the following code to it :</p><pre>private func getTokenFromCallbackURL(request: URLRequest) -&gt; String? {</pre><pre>  let requestURLString = (request.url?.absoluteString)! as String</pre><pre>  if requestURLString.starts(with: “\(redirectURI)?code=”) {</pre><pre>    print(“Response uri:”,requestURLString)</pre><pre>    if let range = requestURLString.range(of: “\(redirectURI)?code=”) {</pre><pre>      return String(requestURLString[range.upperBound…].dropLast(2))</pre><pre>    }</pre><pre>  }</pre><pre>  return nil</pre><pre>}</pre><p>Add another private function <strong>getFormBody</strong> which will return a form-data made by the key value pairs. Add the following code to it :</p><pre>private func getFormBody(_ parameters: [[String : String]], _ boundary: String) -&gt; Data {</pre><pre>  var body = “”</pre><pre>  let error: NSError? = nil</pre><pre>  for param in parameters {</pre><pre>    let paramName = param[“name”]!</pre><pre>    body += “ — \(boundary)\r\n”</pre><pre>    body += “Content-Disposition:form-data; name=\”\(paramName)\””</pre><pre>    if let filename = param[“fileName”] {</pre><pre>      let contentType = param[“content-type”]!</pre><pre>      var fileContent: String = “”</pre><pre>       do { <br>        fileContent = try String(contentsOfFile: filename, encoding: String.Encoding.utf8)<br>      } catch {</pre><pre>        print(error)</pre><pre>      }</pre><pre>      if (error != nil) {</pre><pre>        print(error!)</pre><pre>      }</pre><pre>      body += “; filename=\”\(filename)\”\r\n”</pre><pre>      body += “Content-Type: \(contentType)\r\n\r\n”</pre><pre>      body += fileContent</pre><pre>    } else if let paramValue = param[“value”] {</pre><pre>      body += “\r\n\r\n\(paramValue)”</pre><pre>    }</pre><pre>  }</pre><pre>  return body.data(using: .utf8)!</pre><pre>}</pre><p>To get the test user token and id we need to call the post method of the api whose curl representation is given below :</p><pre>curl -X POST \</pre><pre><a href="https://api.instagram.com/oauth/access_token">https://api.instagram.com/oauth/access_token</a> \</pre><pre>-F app_id={app-id} \</pre><pre>-F app_secret={app-secret} \</pre><pre>-F grant_type=authorization_code \</pre><pre>-F redirect_uri={redirect-uri} \</pre><pre>-F code={code}</pre><p>Upon success, the API will return a JSON encoded object containing an Instagram User Access Token and your Instagram test user’s ID:</p><pre>{</pre><pre>  “access_token”: “IGQVJ…”,</pre><pre>  “user_id”: 17841405793187218</pre><pre>}</pre><p>Now, inside your <strong>InstagramApi</strong> class create a new function <strong>getTestUserIDAndToken</strong>, and add the following code inside :</p><pre>func getTestUserIDAndToken(request: URLRequest, completion: @escaping (InstagramTestUser) -&gt; Void){</pre><pre>  guard let authToken = getTokenFromCallbackURL(request: request) else {</pre><pre>  return</pre><pre>  }</pre><pre>  let headers = [</pre><pre>    “content-type”: “multipart/form-data; boundary=\(boundary)”</pre><pre>  ]</pre><pre>  let parameters = [</pre><pre>    [</pre><pre>      “name”: “app_id”,</pre><pre>      “value”: instagramAppID</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “app_secret”,</pre><pre>      “value”: app_secret</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “grant_type”,</pre><pre>      “value”: “authorization_code”</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “redirect_uri”,</pre><pre>      “value”: redirectURI</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “code”,</pre><pre>      “value”: authToken</pre><pre>    ]</pre><pre>  ]</pre><pre>  var request = URLRequest(url: URL(string: BaseURL.displayApi.rawValue + Method.access_token.rawValue)!)</pre><pre>  let postData = getFormBody(parameters, boundary)</pre><pre>  request.httpMethod = “POST”</pre><pre>  request.allHTTPHeaderFields = headers</pre><pre>  request.httpBody = postData</pre><pre>  let session = URLSession.shared</pre><pre>  let dataTask = session.dataTask(with: request, completionHandler: {(data, response, error) in</pre><pre>    if (error != nil) { </pre><pre>      print(error!)</pre><pre>    } else {</pre><pre>      do { <br>      let jsonData = try JSONDecoder().decode(InstagramTestUser.self, from: data!)</pre><pre>        print(jsonData)</pre><pre>        completion(jsonData)</pre><pre>      } catch let error as NSError {</pre><pre>        print(error)</pre><pre>      }</pre><pre>    }</pre><pre>  })</pre><pre>  dataTask.resume()</pre><pre>}</pre><p>Now, go inside your <strong>WebViewController</strong> file ,inside the decidePolicyFor navigationAction method, add the following lines of code to call the getTestUserIDAndToken method :</p><pre>func webView(<strong>_</strong> webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {</pre><pre>  let request = navigationAction.request</pre><pre>  self.instagramApi?.getTestUserIDAndToken(request: request) { [weak self] (instagramTestUser) in</pre><pre>    self?.testUserData = instagramTestUser</pre><pre>    DispatchQueue.main.async {</pre><pre>      self?.dismissViewController()</pre><pre>    }</pre><pre>  }</pre><pre>  decisionHandler(WKNavigationActionPolicy.allow)</pre><pre>}</pre><p>This will get the Test user’s ID and Token and save it in the binding variable testUserData.</p><h3>Get the user’s feed</h3><h3>Query the User node</h3><p>Now, you need to get the instagram user’s(who granted your app permission) id and username to get it’s feed.</p><p>We can get this data by the GET method of the InstagramApi given below :</p><pre>“https://graph.instagram.com/{user-id}?fields=id,username&amp;access_token={access-token}”</pre><p>Upon success, the API will respond with your Instagram user’s ID and username:</p><pre>{</pre><pre>  “id”: “17841405793187218”,</pre><pre>  “username”: “username_xyz”</pre><pre>}</pre><p>Now, inside your <strong>InstagramApi</strong> class create a function <strong>getInstagramUser</strong>, and add the following code to it :</p><pre>func getInstagramUser(testUserData: InstagramTestUser, completion: @escaping (InstagramUser) -&gt; Void) {</pre><pre>  let urlString = “\(BaseURL.graphApi.rawValue)\(testUserData.user_id)?fields=id,username&amp;access_token=\(testUserData.access_token)”</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let dataTask = session.dataTask(with: request, completionHandler: {(data, response, error) in</pre><pre>    if (error != nil) {</pre><pre>      print(error!)</pre><pre>    } else {</pre><pre>    let httpResponse = response as? HTTPURLResponse</pre><pre>    print(httpResponse!)</pre><pre>    }</pre><pre>    do { <br>      let jsonData = try JSONDecoder().decode(InstagramUser.self, from: data!)</pre><pre>      completion(jsonData)</pre><pre>    } catch let error as NSError {</pre><pre>      print(error)</pre><pre>    }</pre><pre>  })</pre><pre>  dataTask.resume()</pre><pre>}</pre><p>Now go into your <strong>ViewController</strong> file and add a variable <strong>instagramUser :</strong></p><pre>var instagramUser: InstagramUser</pre><p>After this add the following lines of code inside the authenticateOrSignIn method:</p><pre>@IBAction func authenticateOrSignIn(<strong>_</strong> sender: UIButton) {</pre><pre>  if self.testUserData.user_id == 0 {</pre><pre>    presentWebViewController()</pre><pre>  } else {</pre><pre>    self.instagramApi.getInstagramUser(testUserData: self.testUserData) { [weak self] (user) in</pre><pre>      self?.instagramUser = user</pre><pre>      self?.signedIn = true</pre><pre>      DispatchQueue.main.async {</pre><pre>        self?.presentAlert()</pre><pre>      }<br>    }</pre><pre>  }<br>}</pre><p>After this add the following method to your ViewController class :</p><pre>func presentAlert() {</pre><pre>  let alert = UIAlertController(title: “Signed In:”, message: “with account: @\(self.instagramUser!.username)”, preferredStyle: UIAlertController.Style.alert)</pre><pre>  alert.addAction(UIAlertAction(title: “OK”, style: UIAlertAction.Style.default, handler: nil))</pre><pre>  self.present(alert, animated: true)</pre><pre>}</pre><p>This will present an alert message on clicking the instagram icon Button once you have authorized the App. Inside the alert message , it will show the username of your instagram account as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/0*QY69EJKD1oVULsbn" /></figure><h3>Get the User’s Media</h3><p>Now you have the ID and the username of the Instagram user’s account, by knowing the user’s ID you can get the user’s details by calling this GET method of the Instagram Api:</p><pre>“https://graph.instagram.com//me/media?fields={fields}&amp;access_token={access-token}”</pre><p>We will use “id,caption” as {fields} in this url parameter.</p><p>It will give the following response :</p><pre>{</pre><pre>  “data”: [</pre><pre>    {</pre><pre>      “id”: “17895695668004550”,</pre><pre>      “caption”: “”</pre><pre>    },</pre><pre>    {</pre><pre>      “id”: “17899305451014820”,</pre><pre>      “caption”: “”  </pre><pre>    },</pre><pre>    {</pre><pre>      “id”: “17896450804038745”,</pre><pre>      “caption”: “”</pre><pre>    },</pre><pre>    {</pre><pre>      “id”: “17881042411086627”,</pre><pre>      “caption”: “”</pre><pre>    }</pre><pre>  ],</pre><pre>  “paging”: {</pre><pre>    “cursors”: {</pre><pre>    “after”: “MTAxN…”,</pre><pre>    “before”: “NDMyN…”</pre><pre>    },</pre><pre>  “next”: “https://graph.faceb...&quot;</pre><pre>  }</pre><pre>}</pre><p>Now, got to your <strong>InstagramApi</strong> class and create a new function getMediaData and add the following lines of code inside it :</p><pre>private func getMediaData(testUserData: InstagramTestUser, completion: @escaping (Feed) -&gt; Void) {</pre><pre>  let urlString = “\(BaseURL.graphApi.rawValue)me/media?fields=id,caption&amp;access_token=\(testUserData.access_token)”</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>    if let response = response {</pre><pre>      print(response)</pre><pre>    }</pre><pre>    do { <br>      let jsonData = try JSONDecoder().decode(Feed.self, from: data!)</pre><pre>      print(jsonData)</pre><pre>      completion(jsonData)</pre><pre>    } catch let error as NSError {</pre><pre>      print(error) </pre><pre>    }</pre><pre>  })</pre><pre>  task.resume()</pre><pre>}</pre><p>This will give you the whole array of your instagram account’s feed starting from the most recent one.</p><p>To fetch a single post, we will use the <strong>id</strong> of one of the post from the feed.</p><p>To get the data of a single post , we will use this GET method of the InstagramApi :</p><pre>“https://graph.instagram.com/{media-id}?fields={fields}&amp;access_token={access-token}”</pre><p>For this we will replace the {fields} by “id,media_type,media_url,username,timestamp”</p><p>It will give the following response :</p><pre>{</pre><pre>  “id”: “17895695668004550”,</pre><pre>  “media_type”: “IMAGE”,</pre><pre>  “media_url”: “https://fb-s-b-a.akamaihd.net/...&quot;,</pre><pre>  “username”: “jayposiris”,</pre><pre>  “timestamp”: “2017–08–31T18:10:00+0000”</pre><pre>}</pre><p>Now, create a new function <strong>getMedia</strong> inside your <strong>InstagramApi</strong> class, and add the following code inside it :</p><pre>func getMedia(testUserData: InstagramTestUser, completion: @escaping (InstagramMedia) -&gt; Void) {</pre><pre>  getMediaData(testUserData: testUserData) { (mediaFeed) in</pre><pre>    let urlString = “\(BaseURL.graphApi.rawValue + mediaFeed.data[0].id)?fields=id,media_type,media_url,username,timestamp&amp;access_token=\(testUserData.access_token)”</pre><pre>    let request = URLRequest(url: URL(string: urlString)!)</pre><pre>    let session = URLSession.shared</pre><pre>    let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>      if let response = response {</pre><pre>        print(response)</pre><pre>      }</pre><pre>      do { <br>        let jsonData = try JSONDecoder().decode(InstagramMedia.self, from: data!)</pre><pre>        print(jsonData)</pre><pre>        completion(jsonData)</pre><pre>      } catch let error as NSError {</pre><pre>        print(error)</pre><pre>      }</pre><pre>    })</pre><pre>    task.resume()</pre><pre>  }</pre><pre>}</pre><p>You can use this feed data to show all of the posts’ data as a List in a TableView, and once the user clicks one of the post items in your list , show them the detailed post.It’s all up to you.</p><p>Here, we will be showing the first post that is the post at index 0 in the MediaFeed.data array(which is the latest post in your instagram account).</p><p>Now, inside the fetchImageToBackground method , call the getMedia method to get the latest post made by the Instagram account you are using. Add the following lines of code inside it.</p><pre>@IBAction func fetchImageToBackground(<strong>_</strong> sender: UIButton) {</pre><pre>  if self.instagramUser != nil {</pre><pre>    self.instagramApi.getMedia(testUserData: self.testUserData) { (media) in</pre><pre>      if media.media_type != MediaType.VIDEO {</pre><pre>        let media_url = media.media_url</pre><pre>        self.instagramApi.fetchImage(urlString: media_url, completion: { (fetchedImage) in</pre><pre>          if let imageData = fetchedImage {</pre><pre>            DispatchQueue.main.async {</pre><pre>            self.backgroundImageView.image = UIImage(data: imageData)</pre><pre>            }</pre><pre>          } else {</pre><pre>            print(“Didn’t fetched the data”)</pre><pre>          }</pre><pre>        })</pre><pre>        print(media_url)</pre><pre>      } else {</pre><pre>        print(“Fetched media is a video”)</pre><pre>       }</pre><pre>    }</pre><pre>  } else {</pre><pre>     print(“Not signed in”)</pre><pre>  }</pre><pre>}</pre><p>Now, create a function <strong>fetchImage</strong> inside your <strong>InstagramApi</strong> class, this function will fetch an image from the given url. Inside this functions body add the following lines of code :</p><pre>func fetchImage(urlString: String, completion: @escaping (Data?) -&gt; Void) {</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>    if let response = response {</pre><pre>      print(response)</pre><pre>    }</pre><pre>  completion(data)</pre><pre>  })</pre><pre>  task.resume()</pre><pre>}</pre><p>Now, on clicking the Fetch Image to background you will see you latest posted Image on the background of your app, as shown in the preview below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/0*9HdWFkYSVi32V2ZE" /></figure><p>Congratulations!!! , you fetched your instagram account’s feed from the Instagram Api provided by Facebook.</p><p>And with that my friend, you are done. Great job!</p><p>You can find the whole code for this app here at : <a href="https://github.com/tushar40/InstaApp_storyboard">https://github.com/tushar40/InstaApp_storyboard</a></p><p>I hope you enjoyed this tutorial. Feel free to post your questions and comments on the forums, I can’t wait for what you guys come up with for your next great app! ;)</p><p>This is a post by Tushar Gusain, an Android/iOS developer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10e94292e280" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using the new Instagram Basic Display API in Xcode using SwiftUI:]]></title>
            <link>https://tushargusain40.medium.com/using-the-new-instagram-basic-display-api-using-swift-5-ios-bf06d93c4d8b?source=rss-246d3cfcb899------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf06d93c4d8b</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[facebook-graph-api]]></category>
            <category><![CDATA[swift-5]]></category>
            <category><![CDATA[i̇nstagram]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Tushar Gusain]]></dc:creator>
            <pubDate>Sat, 14 Mar 2020 08:53:35 GMT</pubDate>
            <atom:updated>2020-05-30T12:07:46.968Z</atom:updated>
            <content:encoded><![CDATA[<p>Learn about URLSession, using the new Instagram Basic Display API, and SwiftUI in this Swift 5 iOS Tutorial.</p><h3>Version:-</h3><p>Swift 5, iOS 13, Xcode 11</p><p>This is a post by Tushar Gusain, an Android/iOS developer.</p><p>Social networks are an indispensable part of our daily lives. Not only do we access social networks via websites like twitter.com, facebook.com or instagram.com, but we also find social features in apps, websites, blogs, video games, and more.</p><p>Integrating some social features into your apps can increase the virality of your app, they can help you identify and retain customers, and can add value to your app.</p><p>Before the <strong>update </strong>starting October 15, 2019, <a href="https://api.instagram.com/v1">https://api.instagram.com/v1</a> was used to get the Instagram User Access Token, Permissions and Data from an Instagram user. After that the api was moved and is now owned by Facebook. You can find more information about the deprecation here: <a href="https://www.instagram.com/developer/">https://www.instagram.com/developer/</a> .</p><p>Keep reading to know how simple it is to integrate the new Instagram API in your app.</p><h3>About the updated Instagram Api</h3><p>Facebook provides two apis to integrate into your app to get the instagram features inside your app.The first one is Instagram <a href="https://developers.facebook.com/docs/instagram-api">Graph API</a> ,which can allow the users of our app to access data in their Instagram Business and Instagram Creator accounts. It is categorised under the Business Apis provided by Facebook.</p><p>The second one (which we will be using in this tutorial) is the Instagram <a href="https://developers.facebook.com/docs/instagram-basic-display-api">Basic Display API</a>. It is categorised under the Consumer Api provided by Facebook , it’ll allow the users of our app to get basic profile information, photos, and videos from their Instagram accounts.This API is intended for non-Business and non-Creator Instagram users.</p><p>As we will be using a non-Business and non-Creator account in this tutorial, therefore we are using the Instagram Basic Display Api.</p><p>For more information on the Instagram Apis follow this link: <a href="https://developers.facebook.com/docs/instagram">https://developers.facebook.com/docs/instagram</a></p><h3>Getting started</h3><p>Before we start you should have your own Facebook developer account , you can create one here : <a href="https://developers.facebook.com/apps">Facebook Developer Account</a> .</p><h3>Step 1: Create a Facebook App</h3><p>Go to <a href="https://developers.facebook.com/">developers.facebook.com</a>, click <strong>My Apps</strong>, and create a new app. Once you have created the app and are in the App Dashboard, navigate to <strong>Settings</strong> <strong>&gt;</strong> <strong>Basic</strong>, scroll to the bottom of page, and click Add Platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/0*Tnb2SM0BB6-h5E4B" /></figure><p>Choose iOS, add your iOS Bundle ID and Shared Secret, and save your changes.</p><p>To get your Bundle ID click on your project in Xcode, choose your <strong>Target</strong>, then click on <strong>General</strong>, you’ll find your Bundle ID inside <strong>Identity</strong> <strong>&gt;</strong> <strong>Display Name.</strong></p><p>For your Shared Secret copy your <strong>App Secret</strong> and paste it inside Shared Secret.</p><h3>Step 2: Configure Instagram Basic Display</h3><p>Click <strong>Products</strong>, locate the Instagram product, and click <strong>Set Up</strong> to add it to your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/0*rCzqVszVtloB3vLX" /></figure><p>Click <strong>Basic Display</strong>, scroll to the bottom of the page, then click <strong>Create New App</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/0*XfULH9nIh_xyoMbT" /></figure><p>Now, inside Display Name field, enter the name of the Facebook app you just created.</p><p>Next in Valid OAuth Redirect URIs, enter your website’s URL or any other website’s URL. Normally this would be a dedicated URI that can capture redirect query string parameters, but for this tutorial you can use any website’s URL.</p><p>For example: <a href="https://www.google.com/">https://www.google.com/</a></p><p>Copy the complete URL somewhere since you will need it in later steps to get authorization codes and access tokens.</p><p>In Deauthorize Callback URL, enter the copied URL again. Eventually you will have to change this to a URL that can handle deauthorization notifications, but for the purposes of this tutorial, you can re-use the above copied URL.</p><p>Do the same forData Deletion Request Callback URL.</p><p>Skip the App Review section for now since you will not be switching the app to Live Mode during the tutorial.</p><h3>Step 3: Add an Instagram Test User</h3><p>Navigate to <strong>Roles</strong> <strong>&gt;</strong> <strong>Roles</strong> and scroll down to the <strong>Instagram Testers</strong> section. Click <strong>Add Instagram Testers</strong> and enter your Instagram account’s username and send the invitation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/0*WuwrF8x95hBhIaSV" /></figure><p>Open a new web browser and go to <a href="http://www.instagram.com/">www.instagram.com</a> and sign into your Instagram account that you just invited. Navigate to <strong>(Profile Icon) &gt; Edit Profile &gt; Apps and Websites &gt; Tester Invites</strong> and accept the invitation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/0*Tp9dkSJZg7ffoSGe" /></figure><p>Note:- Do not add any profile in the <strong>Testers </strong>section, as we are not testing the facebook app here.</p><p>Your Instagram account is now eligible to be accessed by your Facebook app while it is in Development Mode.</p><h3>Setting up your Xcode project</h3><h3>Step 1: Create an Xcode project</h3><p>Open <strong>Xcode</strong>, click on <strong>New project</strong>, then select <strong>Single View App</strong> and click <strong>Next</strong>.</p><p>Now, Enter the name of your App in the <strong>Product Name</strong> field, then, select <strong>Swift </strong>as Language and <strong>SwiftUI </strong>as User Interface.</p><p>After that click on <strong>Next</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/0*JLHwX4sYoBeyaVne" /></figure><h3>Step 2: Create the InstagramApi class</h3><p>Now, create a new <strong>Swift file</strong>, name it <strong>InstagramApi</strong>, then inside the file declare a <strong>class InstagramApi</strong>, this class will be responsible for handling api requests. Getting the oauth token, fetching your feed, media etc.</p><pre>class InstagramApi {</pre><pre>  static let shared = InstagramApi()</pre><pre>  private let instagramAppID = “{your-instagram-app-id}”</pre><pre>  private let redirectURIURLEncoded = “https%3A%2F%2Fwww.hotcocoasoftware.com%2F&quot;</pre><pre>  private let redirectURI = “https://www.hotcocoasoftware.com/&quot;</pre><pre>  private let app_secret = “{your-app-secret}”</pre><pre>  private let boundary = “boundary=\(NSUUID().uuidString)”</pre><pre>  private init () {}</pre><pre>}</pre><p>Now, copy and paste the above code inside your InstagramApi class.</p><p>Replace {your-instagram-app-id} with the instagram app id in your facebook app.</p><p>(Note:- Do not confuse instagram app id with facebook app id, your instagram app id is given inside <strong>Products &gt; Instagram&gt; Basic Display &gt; Instagram App ID</strong> )</p><p>Then , replace {your-app-secret} with your Instagram app secret.</p><p>Here, I have used <a href="https://www.hotcocoasoftware.com/">https://www.hotcocoasoftware.com/</a> as the redirect URI , you can use any URI you want for this tutorial.</p><p>For example, if you use <a href="https://www.google.com/">https://www.google.com/</a> as redirectURI your then change the redirectURIURLEncoded’s value to “https%3A%2F%2Fwww.google.com%2F&quot;.</p><p>Now, declare the below enums inside InstagramApi class :</p><pre>private enum BaseURL: String {</pre><pre>  case displayApi = “https://api.instagram.com/&quot;</pre><pre>  case graphApi = “https://graph.instagram.com/&quot;</pre><pre>}</pre><pre>private enum Method: String {</pre><pre>  case authorize = “oauth/authorize”</pre><pre>  case access_token = “oauth/access_token”</pre><pre>}</pre><h3>Step 3: Create a model file</h3><p>Now, create a new <strong>swift file</strong> , name it <strong>InstagramResponse. </strong>Inside the file declare the below structs<strong>:</strong></p><pre>struct InstagramTestUser: Codable {</pre><pre>  var access_token: String</pre><pre>  var user_id: Int</pre><pre>}</pre><pre>struct InstagramUser: Codable {</pre><pre>  var id: String</pre><pre>  var username: String</pre><pre>}</pre><pre>struct Feed: Codable {</pre><pre>  var data: [MediaData]</pre><pre>  var paging: PagingData</pre><pre>}</pre><pre>struct MediaData: Codable {</pre><pre>  var id: String</pre><pre>  var caption: String?</pre><pre>}</pre><pre>struct PagingData: Codable {</pre><pre>  var cursors: CursorData</pre><pre>  var next: String</pre><pre>}</pre><pre>struct CursorData: Codable {</pre><pre>  var before: String</pre><pre>  var after: String</pre><pre>}</pre><pre>struct InstagramMedia: Codable {</pre><pre>  var id: String</pre><pre>  var media_type: MediaType</pre><pre>  var media_url: String</pre><pre>  var username: String</pre><pre>  var timestamp: String</pre><pre>}</pre><pre>enum MediaType: String, Codable {</pre><pre>  case IMAGE</pre><pre>  case VIDEO</pre><pre>  case CAROUSEL_ALBUM</pre><pre>}</pre><h3>Step 4: Setting up the content view</h3><p>(If you want to use the UIStoryboard instead of swiftUI you can follow this post :- <a href="https://medium.com/@tushargusain40/using-the-new-instagram-basic-display-api-using-swift-5-ios-10e94292e280?sk=49f9eb63531bddd457519aec99b7c526">https://medium.com/@tushargusain40/using-the-new-instagram-basic-display-api-using-swift-5-ios-10e94292e280?sk=49f9eb63531bddd457519aec99b7c526</a> )</p><p>Inside your <strong>ContentView</strong> class , add the following code above var body :</p><pre>@State var instagramApi = InstagramApi.shared</pre><pre>@State var signedIn = false</pre><pre>@State var instagramImage = UIImage(imageLiteralResourceName: <br>“instagram_background”)</pre><p>Then, inside <strong>var body: some View</strong>, add the following code :</p><p>NavigationView {</p><pre>NavigationView {</pre><pre>  ZStack {</pre><pre>    Image(uiImage: instagramImage)</pre><pre>      .resizable()</pre><pre>      .aspectRatio(contentMode: .fill)</pre><pre>      .edgesIgnoringSafeArea(Edge.Set.all)</pre><pre>    VStack{</pre><pre>      Button(action: {</pre><pre>        //get instagram user data</pre><pre>      }) {</pre><pre>        Image(“Instagram_icon”)</pre><pre>          .renderingMode(.original)</pre><pre>          .resizable()</pre><pre>          .scaledToFill()</pre><pre>          .frame(width: 100, height: 100)</pre><pre>      }</pre><pre>      Button(action: {</pre><pre>        //get instagram media</pre><pre>      }){</pre><pre>        Text(“Fetch Media to background”)</pre><pre>          .font(.headline)</pre><pre>          .padding()</pre><pre>      }</pre><pre>    }</pre><pre>  }</pre><pre>}</pre><p>After this, Download image from google and name it “instagram_background”, this will serve as the background image for your app.</p><p>Also, download an instagram icon from google and name it “Instagram_icon”.</p><p>This will create a button which will be used to authorize and sign in the user,</p><p>Below it will be a button Fetch Media to background which will set your background with the latest image you posted with your instagram account.</p><p>It will look like this in the preview:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*KP_Q1eZsPdZTX6do" /></figure><p>Now , you are all set to call the Instagram Api.</p><h3>Getting your Access Token</h3><h3>Get Authorization</h3><p>We need to authenticate the Test User, to do this we have to call the below url :</p><pre><a href="https://api.instagram.com/oauth/authorize">https://api.instagram.com/oauth/authorize</a></pre><pre>?app_id={app-id}</pre><pre>&amp;redirect_uri={redirect-uri-urlencoded}</pre><pre>&amp;scope=user_profile,user_media</pre><pre>&amp;response_type=code</pre><p>This will provide you with an authorization window, showing you the test user id that you added in the instagram app.</p><p>Go inside your <strong>InstagramApi</strong> class, create a function <strong>authorizeApp</strong> and paste the following code:</p><pre>func authorizeApp(completion: @escaping (_ url: URL?) -&gt; Void ) {</pre><pre>  let urlString = “\(BaseURL.displayApi.rawValue)\. (Method.authorize.rawValue)?app_id=\(instagramAppID)&amp;redirect_uri=\ (redirectURIURLEncoded)&amp;scope=user_profile,user_media&amp;response_type=code”</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let task = session.dataTask(with: request, completionHandler: {  data, response, error in</pre><pre>    if let response = response {</pre><pre>      print(response)</pre><pre>      completion(response.url)</pre><pre>    }</pre><pre>  })</pre><pre>  task.resume()</pre><pre>}</pre><p>This function will call the above URL for authorization.</p><p>Now, go to the ContentView class and add the following two variables :</p><pre>@State var presentAuth = false</pre><pre>@State var testUserData = InstagramTestUser(access_token: “”, user_id: 0)</pre><p>After this create a new <strong>Swift UI View</strong> file, name it <strong>WebView. </strong>Then, add the following variables to the class:</p><pre>import SwiftUI</pre><pre>import WebKit</pre><pre>struct WebView: UIViewRepresentable {</pre><pre>  //MARK:- Member variables</pre><pre>  @Binding var presentAuth: Bool</pre><pre>  @Binding var testUserData: InstagramTestUser</pre><pre>  @Binding var instagramApi: InstagramApi</pre><p>Add the UIViewRepresentable delegate methods:</p><pre>//MARK:- UIViewRepresentable Delegate Methods</pre><pre>func makeCoordinator() -&gt; WebView.Coordinator {</pre><pre>  return Coordinator(parent: self)</pre><pre>}</pre><pre>func makeUIView(context: UIViewRepresentableContext&lt;WebView&gt;) -&gt; WKWebView {</pre><pre>  // return a webView object</pre><pre>}</pre><pre>func updateUIView(_ webView: WKWebView, context: UIViewRepresentableContext&lt;WebView&gt;) {</pre><pre>  //authorize the app</pre><pre>}</pre><p>Initialize a <strong>Coordinator</strong> class inside <strong>WebView </strong>, and add the following code :</p><pre>//MARK:- Coordinator class</pre><pre>class Coordinator: NSObject, WKNavigationDelegate {</pre><pre>  var parent: WebView</pre><pre>  init(parent: WebView) {</pre><pre>  self.parent = parent</pre><pre>  }</pre><pre>  func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {</pre><pre>  // get instagram test user token and id</pre><pre>  }<br>}</pre><p>Now, initialize and return a new WKWebView from the <strong>makeUIView</strong> function:</p><pre>func makeUIView(context: UIViewRepresentableContext&lt;WebView&gt;) -&gt; WKWebView {</pre><pre>  let webView = WKWebView()</pre><pre>  webView.navigationDelegate = context.coordinator</pre><pre>  return webView</pre><pre>}</pre><p>Call the <strong>InstagramApi</strong>’s authorizeApp function from updateUIView:</p><pre>func updateUIView(_ webView: WKWebView, context: UIViewRepresentableContext&lt;WebView&gt;) {</pre><pre>  instagramApi.authorizeApp { (url) in</pre><pre>    DispatchQueue.main.async {</pre><pre>      webView.load(URLRequest(url: url!))</pre><pre>    }</pre><pre>  }</pre><pre>}</pre><p>Now go inside your ContentView class and inside the var body, add the following code as the Button action present the authorization window when the instagram icon is clicked:</p><pre>Image(uiImage: instagramImage)</pre><pre>  .resizable()</pre><pre>  .aspectRatio(contentMode: .fill)</pre><pre>  .edgesIgnoringSafeArea(Edge.Set.all)</pre><pre>VStack{</pre><pre>  Button(action: {</pre><pre>    if self.testUserData.user_id == 0 {</pre><pre>    self.presentAuth.toggle()</pre><pre>    } else {</pre><pre>      // get the signed in user details</pre><pre>    }</pre><pre>  }) {</pre><pre>    Image(“Instagram_icon”)</pre><pre>      .renderingMode(.original)</pre><pre>      .resizable()</pre><pre>      .scaledToFill()</pre><pre>      .frame(width: 100, height: 100)</pre><pre>}</pre><p>At the end of your NavigationView’s body add a sheet and paste the following code:</p><pre>.sheet(isPresented: self.$presentAuth) {</pre><pre>  WebView(presentAuth: self.$presentAuth, testUserData:   self.$testUserData, instagramApi: self.$instagramApi)</pre><pre>}</pre><p>Now, run the app , clicking the instagram icon will present one of the following sheet as shown in the images below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*AAh1apndXXOkDaSn" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*Rc_EbYTwmSH0d7ME" /></figure><p>Enter your username &amp; password(or click on the continue button if you got the second screen) to authorise the test user and grant your app access to your profile data. Upon success, the page will redirect you to the redirect URI you included in the previous step and append an Authorization Code. For example:</p><pre><a href="https://hotcocoasoftware.com/auth/?code=AQDp3TtBQQ...#_">https://hotcocoasoftware.com/auth/?code=AQDp3TtBQQ...#_</a></pre><p>Note:- #_ has been appended to the end of the redirect URI, but it is not part of the code itself.</p><h3>Exchange the Code For a Token</h3><p>Inside your <strong>InstagramApi</strong> class create a new function <strong>getTokenFromCallbackURL</strong>, and add the following code to it :</p><pre>private func getTokenFromCallbackURL(request: URLRequest) -&gt; String? {</pre><pre>  let requestURLString = (request.url?.absoluteString)! as String</pre><pre>  if requestURLString.starts(with: “\(redirectURI)?code=”) {</pre><pre>    print(“Response uri:”,requestURLString)</pre><pre>    if let range = requestURLString.range(of: “\(redirectURI)?code=”) {</pre><pre>      return String(requestURLString[range.upperBound…].dropLast(2))</pre><pre>    }</pre><pre>  }</pre><pre>  return nil</pre><pre>}</pre><p>Add another private function <strong>getFormBody</strong> which will return a form-data made by the key value pairs. Add the following code to it :</p><pre>private func getFormBody(_ parameters: [[String : String]], _ boundary: String) -&gt; Data {</pre><pre>  var body = “”</pre><pre>  let error: NSError? = nil</pre><pre>  for param in parameters {</pre><pre>    let paramName = param[“name”]!</pre><pre>    body += “ — \(boundary)\r\n”</pre><pre>    body += “Content-Disposition:form-data; name=\”\(paramName)\””</pre><pre>    if let filename = param[“fileName”] {</pre><pre>      let contentType = param[“content-type”]!</pre><pre>      var fileContent: String = “”</pre><pre>      do { <br>        fileContent = try String(contentsOfFile: filename, encoding: String.Encoding.utf8)<br>      }catch {</pre><pre>        print(error)</pre><pre>      }</pre><pre>      if (error != nil) {</pre><pre>        print(error!)</pre><pre>      }</pre><pre>      body += “; filename=\”\(filename)\”\r\n”</pre><pre>      body += “Content-Type: \(contentType)\r\n\r\n”</pre><pre>      body += fileContent</pre><pre>    } else if let paramValue = param[“value”] {</pre><pre>      body += “\r\n\r\n\(paramValue)”</pre><pre>    }</pre><pre>  }</pre><pre>  return body.data(using: .utf8)!</pre><pre>}</pre><p>To get the test user token and id we need to call the post method of the api whose curl representation is given below :</p><pre>curl -X POST \</pre><pre><a href="https://api.instagram.com/oauth/access_token">https://api.instagram.com/oauth/access_token</a> \</pre><pre>-F app_id={app-id} \</pre><pre>-F app_secret={app-secret} \</pre><pre>-F grant_type=authorization_code \</pre><pre>-F redirect_uri={redirect-uri} \</pre><pre>-F code={code}</pre><p>Upon success, the API will return a JSON encoded object containing an Instagram User Access Token and your Instagram test user’s ID:</p><pre>{</pre><pre>“access_token”: “IGQVJ…”,</pre><pre>“user_id”: 17841405793187218</pre><pre>}</pre><p>Now, inside your <strong>InstagramApi</strong> class create a new function <strong>getTestUserIDAndToken</strong>, and add the following code inside :</p><pre>func getTestUserIDAndToken(request: URLRequest, completion: @escaping (InstagramTestUser) -&gt; Void){</pre><pre>  guard let authToken = getTokenFromCallbackURL(request: request)   else {</pre><pre>    return</pre><pre>  }</pre><pre>  let headers = [</pre><pre>    “content-type”: “multipart/form-data; boundary=\(boundary)”</pre><pre>  ]</pre><pre>  let parameters = [</pre><pre>    [</pre><pre>      “name”: “app_id”,</pre><pre>      “value”: instagramAppID</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “app_secret”,</pre><pre>      “value”: app_secret</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “grant_type”,</pre><pre>      “value”: “authorization_code”</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “redirect_uri”,</pre><pre>      “value”: redirectURI</pre><pre>    ],</pre><pre>    [</pre><pre>      “name”: “code”,</pre><pre>      “value”: authToken</pre><pre>    ]</pre><pre>  ]</pre><pre>  var request = URLRequest(url: URL(string:   BaseURL.displayApi.rawValue + Method.access_token.rawValue)!)</pre><pre>  let postData = getFormBody(parameters, boundary)</pre><pre>  request.httpMethod = “POST”</pre><pre>  request.allHTTPHeaderFields = headers</pre><pre>  request.httpBody = postData</pre><pre>  let session = URLSession.shared</pre><pre>  let dataTask = session.dataTask(with: request, completionHandler: {(data, response, error) in</pre><pre>    if (error != nil) {</pre><pre>      print(error!)</pre><pre>    } else {</pre><pre>      do { let jsonData = try JSONDecoder().decode(InstagramTestUser.self, from: data!)</pre><pre>        print(jsonData)</pre><pre>        completion(jsonData)</pre><pre>      } catch let error as NSError {</pre><pre>        print(error)</pre><pre>      }</pre><pre>    }</pre><pre>  })</pre><pre>  dataTask.resume()</pre><pre>}</pre><p>Now, inside your <strong>WebView.Coordinator</strong> class’ method decidePolicyFor navigationAction, add the following lines of code to call the getTestUserIDAndToken method :</p><pre>func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {</pre><pre>  let request = navigationAction.request</pre><pre>  self.parent.instagramApi.getTestUserIDAndToken(request: request) { (instagramTestUser) in</pre><pre>  self.parent.testUserData = instagramTestUser</pre><pre>  self.parent.presentAuth = false</pre><pre>  }</pre><pre>  decisionHandler(WKNavigationActionPolicy.allow)</pre><pre>}</pre><p>This will get the Test user’s ID and Token and save it in the binding variable testUserData.</p><h3>Get the user’s feed</h3><h3>Query the User node</h3><p>Now, you need to get the instagram user’s(who granted your app permission) id and username to get it’s feed.</p><p>We can get this data by the GET method of the InstagramApi given below :</p><pre>“https://graph.instagram.com/{user-id}?fields=id,username&amp;access_token={access-token}”</pre><p>Upon success, the API will respond with your Instagram user’s ID and username:</p><pre>{</pre><pre>  “id”: “17841405793187218”,</pre><pre>  “username”: “username_xyz”</pre><pre>}</pre><p>Now, inside your <strong>InstagramApi</strong> class create a function <strong>getInstagramUser</strong>, and add the following code to it :</p><pre>func getInstagramUser(testUserData: InstagramTestUser, completion: @escaping (InstagramUser) -&gt; Void) {</pre><pre>  let urlString = “\(BaseURL.graphApi.rawValue)\(testUserData.user_id)?fields=id,username&amp;access_token=\(testUserData.access_token)”</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let dataTask = session.dataTask(with: request, completionHandler: {(data, response, error) in</pre><pre>    if (error != nil) {</pre><pre>      print(error!)</pre><pre>    } else {</pre><pre>      let httpResponse = response as? HTTPURLResponse</pre><pre>      print(httpResponse!)</pre><pre>    }</pre><pre>    do { let jsonData = try JSONDecoder().decode(InstagramUser.self, from: data!)</pre><pre>      completion(jsonData)</pre><pre>    }catch let error as NSError {</pre><pre>      print(error)</pre><pre>    }</pre><pre>  })</pre><pre>  dataTask.resume()</pre><pre>}</pre><p>Now go into your <strong>ContentView</strong> class and add a state variable <strong>instagramUser :</strong></p><pre>@State var instagramUser: InstagramUser? = nil</pre><p>After this add the following lines of code inside the instagram icon Button’s action part :</p><pre>Button(action: {</pre><pre>  if self.testUserData.user_id == 0 {</pre><pre>    self.presentAuth.toggle()</pre><pre>  } else {</pre><pre>    self.instagramApi.getInstagramUser(testUserData: self.testUserData) { (user) in</pre><pre>      self.instagramUser = user</pre><pre>      self.signedIn.toggle()</pre><pre>    }</pre><pre>  }</pre><pre>}) {</pre><pre>  Image(“Instagram_icon”)</pre><pre>    .renderingMode(.original)</pre><pre>    .resizable()</pre><pre>    .scaledToFill()</pre><pre>    .frame(width: 100, height: 100)</pre><pre>}</pre><p>After this add an action sheet at the end your <strong>NavigationView</strong>’s body :</p><pre>}.sheet(isPresented: self.$presentAuth) {</pre><pre>  WebView(presentAuth: self.$presentAuth, testUserData:   self.$testUserData, instagramApi: self.$instagramApi)</pre><pre>}</pre><pre>.actionSheet(isPresented: self.$signedIn) {</pre><pre>  let actionSheet = ActionSheet(title: Text(“Signed in:”), message: Text(“with account: @\(self.instagramUser!.username)”),buttons:   [.default(Text(“OK”))])</pre><pre>  return actionSheet</pre><pre>}</pre><p>This will show an action sheet on clicking the instagram icon Button once you have authorized the App. Inside the Actionsheet , it will show the username of your instagram account as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*dJJ2W0E3GegxCW39" /></figure><h3>Get the User’s Media</h3><p>Now you have the ID and the username of the Instagram user’s account, by knowing the user’s ID you can get the user’s details by calling this GET method og the Instagram Api:</p><pre>“https://graph.instagram.com//me/media?fields={fields}&amp;access_token={access-token}”</pre><p>We will use “id,caption” as {fields} in this url parameter.</p><p>It will give the following response :</p><pre>{</pre><pre>  “data”: [</pre><pre>    {</pre><pre>       “id”: “17895695668004550”,</pre><pre>        “caption”: “”</pre><pre>     },</pre><pre>     {</pre><pre>       “id”: “17899305451014820”,</pre><pre>        “caption”: “”</pre><pre>      },</pre><pre>      {</pre><pre>        “id”: “17896450804038745”,</pre><pre>        “caption”: “”</pre><pre>      },</pre><pre>      {</pre><pre>         “id”: “17881042411086627”,</pre><pre>         “caption”: “”</pre><pre>      }</pre><pre>    ],</pre><pre>  “paging”: {</pre><pre>    “cursors”: {</pre><pre>    “after”: “MTAxN…”,</pre><pre>    “before”: “NDMyN…”</pre><pre>    },</pre><pre>    “next”: “https://graph.faceb...&quot;</pre><pre>  }</pre><pre>}</pre><p>Now, got to your <strong>InstagramApi</strong> class and create a new function getMediaData and add the following lines of code inside it :</p><pre>private func getMediaData(testUserData: InstagramTestUser, completion: @escaping (Feed) -&gt; Void) {</pre><pre>  let urlString = “\(BaseURL.graphApi.rawValue)me/media? fields=id,caption&amp;access_token=\(testUserData.access_token)”</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>    if let response = response {</pre><pre>      print(response)</pre><pre>    }</pre><pre>    do { let jsonData = try JSONDecoder().decode(Feed.self, from: data!)</pre><pre>      print(jsonData)</pre><pre>      completion(jsonData)</pre><pre>    } catch let error as NSError {</pre><pre>      print(error)</pre><pre>    }</pre><pre>  })</pre><pre>  task.resume()</pre><pre>}</pre><p>This will give you the whole array of your instagram account’s feed starting from the most recent one.</p><p>To fetch a single post, we will use the <strong>id</strong> of one of the post from the feed.</p><p>To get the data of a single post , we will use this GET method of the InstagramApi :</p><pre>“https://graph.instagram.com/{media-id}?fields={fields}&amp;access_token={access-token}”</pre><p>For this we will replace the {fields} by “id,media_type,media_url,username,timestamp”</p><p>It will give the following response :</p><pre>{</pre><pre>  “id”: “17895695668004550”,</pre><pre>  “media_type”: “IMAGE”,</pre><pre>  “media_url”: “https://fb-s-b-a.akamaihd.net/...&quot;,</pre><pre>  “username”: “jayposiris”,</pre><pre>  “timestamp”: “2017–08–31T18:10:00+0000”</pre><pre>}</pre><p>Now, create a new function <strong>getMedia</strong> inside your <strong>InstagramApi</strong> class, and add the following code inside it :</p><pre>func getMedia(testUserData: InstagramTestUser, completion: @escaping (InstagramMedia) -&gt; Void) {</pre><pre>  getMediaData(testUserData: testUserData) { (mediaFeed) in</pre><pre>    let urlString = “\(BaseURL.graphApi.rawValue + mediaFeed.data[0].id)?fields=id,media_type,media_url,username,timestamp&amp;access_token=\(testUserData.access_token)”</pre><pre>    let request = URLRequest(url: URL(string: urlString)!)</pre><pre>    let session = URLSession.shared</pre><pre>    let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>      if let response = response {</pre><pre>        print(response)</pre><pre>      }</pre><pre>      do { let jsonData = try JSONDecoder().decode(InstagramMedia.self, from: data!)</pre><pre>        print(jsonData) </pre><pre>        completion(jsonData)</pre><pre>      } catch let error as NSError {</pre><pre>         print(error)</pre><pre>      }</pre><pre>    })</pre><pre>    task.resume()</pre><pre>  }</pre><pre>}</pre><p>You can use this feed data to show all of the posts’ data as a List in a TableView, and once the user clicks one of the post items in your list , show them the detailed post.It’s all up to you.</p><p>Here, we will be showing the first post that is the post at index 0 in the MediaFeed.data array(which is the latest post in your instagram account).</p><p>Now, inside the fetch media text <strong>Button</strong>’s action closure , call the getMedia method to get the latest post made by the Instagram account you are using.Add the following lines of code inside it.</p><pre>Button(action: {</pre><pre>  if self.instagramUser != nil {</pre><pre>    self.instagramApi.getMedia(testUserData: self.testUserData) { (media) in</pre><pre>      if media.media_type != MediaType.VIDEO {</pre><pre>        let media_url = media.media_url</pre><pre>        self.instagramApi.fetchImage(urlString: media_url, completion: { (fetchedImage) in</pre><pre>          if let imageData = fetchedImage {</pre><pre>            self.instagramImage = UIImage(data: imageData)!</pre><pre>          } else {</pre><pre>            print(“Didn’t fetched the data”)</pre><pre>          }</pre><pre>        })</pre><pre>        print(media_url)</pre><pre>      } else {</pre><pre>        print(“Fetched media is a video”)</pre><pre>      }</pre><pre>    }</pre><pre>  } else {</pre><pre>      print(“Not signed in”)</pre><pre>  }</pre><pre>}){</pre><pre> Text(“Fetch Media to background”)</pre><pre>   .font(.headline)<br>   .padding()</pre><pre>}</pre><p>Now, create a function <strong>fetchImage</strong> inside your <strong>InstagramApi</strong> class, this function will fetch an image from the given url. Inside this functions body add the following lines of code :</p><pre>func fetchImage(urlString: String, completion: @escaping (Data?) -&gt; Void) {</pre><pre>  let request = URLRequest(url: URL(string: urlString)!)</pre><pre>  let session = URLSession.shared</pre><pre>  let task = session.dataTask(with: request, completionHandler: { data, response, error in</pre><pre>    if let response = response {</pre><pre>      print(response)</pre><pre>    }</pre><pre>    completion(data)</pre><pre>  })</pre><pre>  task.resume()</pre><pre>}</pre><p>Now, on clicking the Fetch Image to background you will see you latest posted Image on the background of your app, as shown in the preview below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/0*CG6gPH-fCBFcT1QI" /></figure><p>Congratulations!!! , you fetched your instagram account’s feed from the Instagram Api provided by Facebook.</p><p>And with that my friend, you are done. Great job!</p><p>You can find the whole code for this app here at : <a href="https://github.com/tushar40/InstaApp-iOS">https://github.com/tushar40/InstaApp-iOS</a></p><p>I hope you enjoyed this tutorial. Feel free to post your questions and comments on the forums, I can’t wait for what you guys come up with for your next great app! ;)</p><p>This is a post by Tushar Gusain, an Android/iOS developer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf06d93c4d8b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>