Saturday 5 December 2020

How to Creating thumbnails in PhoneGap?

 In our previous section, we learned about the options for taking the pictures and how we can take the pictures using PhoneGap. In this section, we will learn about how we can create thumbnails with our images. It is very easy to create thumbnails using PhoneGap. We will use our previous example and make some changes to it.

These are the following steps used to obtain the image using PhoneGap:

1) Create index2.html

We will create a new file index2.html with the same code as preset in the index.html. We will make changes later in index2.html file not in index.html.

Creating thumbnails

2) Create checkboxes

Now, we will create checkboxes for thumbnail and high quality. If the user will check the thumbnail checkbox and take a picture from camera using PhoneGap then the picture will be shown as compressed picture. If the user will check the high quality checkbox and the take a picture from camera using PhoneGap then the picture will be shown as high quality picture. We will use the <input></input> tags for creating checkboxes and give labels to it in the following way:

  1. <label for="thumbs">Thumbnails</label>  
  2. <input type="checkbox" id="thumbs" />  
  3. <label for="hq">High Quality</label>  
  4. <input type="checkbox" id="hq" />  


Creating thumbnails

3) Make changes in takePic function.

Now, we will convert the image either into a thumbnail or high quality. We will use the JQuery for this purpose. If the Thumbnail checkbox is checked, the width will set the width to 150 and height to 100. This will create the thumbnail of the picture.

If the "high quality" checkbox will be checked, we set the quality option to 90. These conditions are added below the options and above the getPicture() function.

  1. //Convert to Thumbnail  
  2. if(( $("#thumbs").is(':checked')))  
  3. {  
  4.             options.targetWidth = 150;  
  5.             options.targetHeight = 100;  
  6. }  
  7.               
  8. if(( $("#hq").is(':checked')))  
  9. {  
  10.             options.quality = 90;  
  11. }  

Now, we are ready to run our project on the PhoneGap Developer App.

Complete Project

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6.         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />      
  7.         <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>  
  8.         <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  
  9.         <style>  
  10.             #imgArea  
  11.             {  
  12.                 border: 1px solid black;  
  13.                 max-width: 600px;  
  14.             }  
  15.         </style>  
  16.         <script>  
  17.         window.onload=function()  
  18.         {  
  19.             document.getElementById('btnTakePicture').addEventListener('click', takePic);  
  20.             document.getElementById('btnClear').addEventListener('click',function(){  
  21.                document.getElementById('imgArea').src="";   
  22.             });  
  23.                                                                    
  24.         }  
  25.           
  26.         function takePic(e)  
  27.         {  
  28.             var options = {  
  29.                 quality: 50,  
  30.                 destinationType: Camera.DestinationType.FILE_URI,  
  31.                 encodingType: Camera.EncodingType.JPEG,  
  32.                 mediaType: Camera.MediaType.PICTURE,  
  33.                 targetWidth: 600,  
  34.                 targetHeight: 400  
  35.           
  36.             }  
  37.             //Convert to Thumbnail  
  38.             if(( $("#thumbs").is(':checked')))  
  39.                 {  
  40.                     options.targetWidth = 150;  
  41.                     options.targetHeight = 100;  
  42.                 }   
  43.             if(( $("#hq").is(':checked')))  
  44.                 {  
  45.                     options.quality = 90;  
  46.                 }   
  47.             navigator.camera.getPicture(success, fail, options);  
  48.         }   
  49.         function success(thePicture)  
  50.         {  
  51.             var image = document.getElementById('imgArea');  
  52.             image.src = thePicture;  
  53.         }  
  54.               
  55.         function fail(e)  
  56.         {  
  57.             alert("Image failed: " + e.message);  
  58.         }  
  59.         </script>  
  60.         <title>Pic Options</title>  
  61.     </head>  
  62.     <body>  
  63.         <img id="imgArea" />  
  64.         <button id="btnTakePicture"> Click Picture </button>  
  65.         <button id="btnClear"> Clear Picture </button>  
  66.         <label for="thumbs"> Thumbnails </label>  
  67.         <input type="checkbox" id="thumbs" />  
  68.         <label for="hq"> High Quality </label>  
  69.         <input type="checkbox" id="hq" />  
  70.         <script type="text/javascript" src="cordova.js"></script>   
  71.     </body>  
  72. </html>  

Output

Creating thumbnails

No comments:

Post a Comment