<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jorge Vergara</title>
    <description>The latest articles on DEV Community by Jorge Vergara (@javebratt).</description>
    <link>https://dev.to/javebratt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F139866%2F00720bc0-fe85-447c-9a27-2e72c3de1309.png</url>
      <title>DEV Community: Jorge Vergara</title>
      <link>https://dev.to/javebratt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/javebratt"/>
    <language>en</language>
    <item>
      <title>Use your phone's camera in Ionic apps with Capacitor</title>
      <dc:creator>Jorge Vergara</dc:creator>
      <pubDate>Fri, 31 Jan 2020 18:28:00 +0000</pubDate>
      <link>https://dev.to/ionic/use-your-phone-s-camera-in-ionic-apps-with-capacitor-44c1</link>
      <guid>https://dev.to/ionic/use-your-phone-s-camera-in-ionic-apps-with-capacitor-44c1</guid>
      <description>&lt;p&gt;We're going to use Capacitor to communicate with the native layer of the phone. In this example, we'll use the Camera API to be able to take pictures with the phone's camera.&lt;/p&gt;

&lt;p&gt;One cool thing we're also going to do is to allow Capacitor to use web APIs if available, which means that if your app is running as a PWA it will also be able to use the camera.&lt;/p&gt;

&lt;p&gt;You can find capacitor's &lt;a href="https://capacitor.ionicframework.com/docs/"&gt;official docs here&lt;/a&gt;, but I'll do my best to explain every bit we need to get our functionality ready.&lt;/p&gt;

&lt;p&gt;Let's start with understanding a bit more about capacitor and its role in mobile development. The easiest way to "&lt;em&gt;get it&lt;/em&gt;" is by seeing it as a replacement for Cordova.&lt;/p&gt;

&lt;p&gt;Capacitor allows you to wrap your app into a native container and helps you communicate with the phone's native capabilities. You can use it to create a native build of your application (iOS and Android).&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Capacitor
&lt;/h2&gt;

&lt;p&gt;The first thing we need to do to '&lt;em&gt;activate&lt;/em&gt;' capacitor is to enable it through the Ionic CLI, so open your terminal (&lt;em&gt;while inside of the project's folder&lt;/em&gt;) and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ionic integrations &lt;span class="nb"&gt;enable &lt;/span&gt;capacitor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will create the necessary files and install the capacitor packages, one thing to keep in mind, it creates a file called &lt;code&gt;capacitor.config.json&lt;/code&gt; where you need to go and edit your &lt;code&gt;appId&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By default, it sets it up to be &lt;code&gt;io.ionic.starter&lt;/code&gt;, It's a good idea to change it to something more on-brand. The usual convention is to go with &lt;code&gt;com.myDomainName.myProjectName&lt;/code&gt; so I'll name it &lt;code&gt;com.javebratt.myApp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before adding the android/ios platforms, we need to generate a build of our project, or else capacitor will have unexpected errors.&lt;/p&gt;

&lt;p&gt;Open your terminal and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ionic build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That command will generate a build of our application inside the &lt;code&gt;www/&lt;/code&gt; folder, which is the folder Capacitor is watching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt; SIDE-NOTE:&lt;/strong&gt; That command generates a development build. If you want to create a production build for your application, add the &lt;code&gt;--prod&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Now we can add our platforms. You can add either iOS or Android by using the commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cap add ios
npx cap add android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember that you need XCode installed on a Mac to build for iOS. Once you add your platforms, it's time to sync them and copy them over the build to the platform's respective folder. You do that by opening up the terminal and typing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx cap &lt;span class="nb"&gt;sync&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the Camera API
&lt;/h2&gt;

&lt;p&gt;Now capacitor is ready to use, and we'll start by connecting the camera API.&lt;/p&gt;

&lt;p&gt;For using the camera API the first thing we need is to import capacitor in the file we're using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Plugins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;CameraResultType&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@capacitor/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Camera&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Plugins&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can create a function that takes the picture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;takePicture&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;profilePicture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Camera&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPhoto&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;allowEditing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;resultType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CameraResultType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;guestPicture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;profilePicture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;base64String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The line &lt;code&gt;Camera.getPhoto({});&lt;/code&gt; opens up the camera for you to take the picture, you can see &lt;a href="https://capacitor.ionicframework.com/docs/apis/camera#api"&gt;all the properties here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;And &lt;code&gt;profilePicture.base64String&lt;/code&gt; returns a &lt;code&gt;base64&lt;/code&gt; string of the picture (&lt;em&gt;This one always makes me giggle because I made the PR for that 😃&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it. You can use that &lt;code&gt;base64&lt;/code&gt; string to upload to your favorite storage provider, (&lt;em&gt;I use Firebase Cloud Storage&lt;/em&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  But Jorge, you said it was going to work on the web
&lt;/h2&gt;

&lt;p&gt;Heck yeah, you can! That's the beauty of using Capacitor.&lt;/p&gt;

&lt;p&gt;Some Capacitor plugins, such as &lt;code&gt;Camera&lt;/code&gt;, have web-based UI available when not running natively.&lt;/p&gt;

&lt;p&gt;For example, calling &lt;code&gt;Camera.getPhoto()&lt;/code&gt; will load a responsive photo-taking experience when running on the web.&lt;/p&gt;

&lt;p&gt;To enable these controls, you must add &lt;code&gt;@ionic/pwa-elements&lt;/code&gt; to your app.&lt;/p&gt;

&lt;p&gt;For that, the first thing to do is to stop our &lt;code&gt;ng serve&lt;/code&gt; or &lt;code&gt;ionic serve&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then in the terminal, you type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @ionic/pwa-elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then go into the &lt;code&gt;main.ts&lt;/code&gt; file and define the custom elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;enableProdMode&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;platformBrowserDynamic&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/platform-browser-dynamic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app/app.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./environments/environment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineCustomElements&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@ionic/pwa-elements/loader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;production&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;enableProdMode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;platformBrowserDynamic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bootstrapModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// Call the element loader after the platform has been bootstrapped&lt;/span&gt;
&lt;span class="nx"&gt;defineCustomElements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is it. Now you have a fully functional way of capturing photos with your camera.&lt;/p&gt;

&lt;p&gt;Got any questions? You can find me on twitter as &lt;a href="https://twitter.com/javebratt"&gt;@javebratt&lt;/a&gt; 😃.&lt;/p&gt;

&lt;p&gt;If you want to get started with Ionic &amp;amp; Firebase to build serverles applications consider enrolling in my &lt;a href="https://javebratt.com/"&gt;free course here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ionic</category>
      <category>capacitor</category>
    </item>
  </channel>
</rss>
