Android TutorialsLearn to implement Capture API with Phonegap

Learn to implement Capture API with Phonegap

In this tutorial we will learn to implement capture api with phonegap and create an application which will capture an image in your mobile and directly upload it in the remote server.

  • To create an application using capture api follow the steps given below :

  1. First of all download and install FileZilla and Xampp server respectively.
  2. Go to c drive -> xampp -> htdocs and create a new folder in it called as capture .
  3. In this capture folder create a new text file called as index.html .
  4. Open this index.html file in Notepad++ window or any other text editor and write the given code .
  5. <!--DOCTYPE HTML-->
    <html>
    <head>
    <title>PhoneGap Capture Application</title>
    
    <style>button{background:#333;color:#fff;border-radius:10px;border:2px #623 solid;padding:5px 10px;}</style>
    	<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    	<script type="text/javascript">
    	// Call With Button
    function takePicture( )
    {
    		navigator.device.capture.captureImage(captureSuccess,captureError);
    	}
    
    // If the capture is successful :
    function captureSuccess(mediaFiles)
    {
    		var i, len;
    		for(i=0,len = mediaFiles.length;i<len; i+=1)
    		{
    			uploadFile(mediaFiles[i]);
    		}
    	}
    			
    	// If error	
    	function captureError(error)
    	{
    		var errMsg = 'An error has occurred' + error.code;
    		navigator.notification.alert(errMsg,null,'oh No!');
    	}
    
    	//Upload To Server
    function uploadFile(mediaFile)
    	{
    		var ft = new FileTransfer(),
    		 path = mediaFile.fullpath,
    		 name = mediaFile.name;
    		ft.upload(
    			path,
    			"http://techguystaging.com/test/upload.php",
    			function (result)
    			{
    				console.log('File Upload: Success!!!');
    			},
    			function (error)
    			{
    				console.log('File upload : Failed!!!');
    			},
    			{fileName: name}						
    		);
    }
    </script>
    			
    		</head> 
    		<body>
    			<button onclick="takePicture();">Take Picture</button>
    		</body>
    	</html>
    

  6. Here are the screen shots of the code :
  7. 1
    2
    3
    4

  8. Now open FileZilla and you will have the following home screen :
  9. 5

  10. Now here you have to enter the Host , Username , Password and click on the Quick Connect button as shown in the image below :
  11. 6

  12. Then at the right hand side in the Filename tab select the public_html folder as shown below :
  13. learn to implement capture api with phonegap 7

  14. After that right click on the public_html and click on create directory :
  15. learn to implement capture api with phonegap 8

  16. Thus a dialog box will appear which will ask for the name of the directory so for example name it test as shown below :
  17. learn to implement capture api with phonegap9

  18. Thus your test folder will be created as shown below :
  19. 10

  20. In this test folder create a new directory called as upload as shown below :
  21. learn to implement capture api with phonegap11

  22. Thus a dialog box will pop up asking for the directory name as shown below :
  23. learn to implement capture api with phonegap12

  24. Hence name it as upload .
  25. Now again right click on the test folder and create a new file as shown below :
  26. learn to implement capture api with phonegap13

  27. Again a dialog box will appear which will ask for file name, so name it as upload.php .
  28. 14

  29. Hence both the files will be created as shown below :
  30. learn to implement capture api with phonegap15

  31. Now drag and drop the upload.php file on the desktop and open it with Notepad ++ and write the following code in it as given below :
  32. 16

  33. Hence after writing the code save your document and now sign in into your Github account by visiting github.com :
  34. 0

  35. So now enter a username ,your email id and password in the respective tabs and click on the Sign up for GitHub button .
  36. After you click on the signup button you will get the following window :
  37. 0.1

  38. Now click on the ‘+’ icon on the right hand side as indicated by the red arrow and then click on the “ New repository ” as indicated by the blue arrow .
  39. 0.2

  40. After clicking on the New repository you will be navigated to the next window ,specify a Repository name ,select a public radio button and click on the Create repository button as shown in the image below :
  41. 0.3

  42. So after clicking on the Create repository button you have the following window :
  43. learn to implement capture api with phonegap0.4

  44. Go to xampp -> htdocs .
  45. In htdocs you have your capture folder created .
  46. Now go to your start menu -> GitBash -> right click on it -> Send To -> Desktop (create a shortcut) of GitBash on Desktop as shown below :
  47. 0.7

  48. So now on Desktop you will have a GitBash icon .
  49. Now Right Click on the GitBash icon on the desktop and go to properties and in the Start in edit box set the path as : c:xampphtdocs as shown in the image.
  50. 0.8

  51. Now Click on Apply and OK button .
  52. Now start the GitBash application from the desktop and you will have the following screen :
  53. learn to implement capture api with phonegap0.9

  54. Now insert the following command as shown below in the gitbash command prompt window..
  55. ls
    cd capture(that is your folder name in htdocs folder)
    ls
    

  56. Thus after inserting the commands you will have the following view of your window :
  57. 0.10

  58. After you get this , insert further commands in the same command prompt window as given below :
  59. git config --global user.name “username of your github account”
    
    git config --global user.email “email of your github account”
    
    git config --list
    

  60. After inserting this command your gitbash command prompt window will have the following look , check whether your username and email you entered has been generated in the list :
  61. learn to implement capture api with phonegap0.11

  62. After this insert the following commands :
  63. git init
    
    ls –a
    
    git add .
    
    git commit –am ‘initial commit’
    

  64. You will have the following window :
  65. learn to implement capture api with phonegap0.12

  66. Now go to your github account where you had created the capture repository and copy the add origin command and push command and paste it in the gitbash command prompt respectively.
  67. 0.13

  68. So after adding the commands you will be asked for your github username and password ,so you will have the following window :
  69. 0.14

  70. Thus now the push operation is complete, so now we are ready to build our application .
  71. Now go to www.build.phonegap.com and click on Sign in tab present at the right.
  72. learn to implement capture api with phonegap1

  73. After clicking on the Sign in button you will get the next page where you have to click on the Sign in with GitHub button as shown by the arrow head ..
  74. 2

  75. After that you will be navigated to the next page where you have to click on the new app button as shown by the red arrow head.
  76. 3

  77. After clicking on the new app button ,you will get the following window where you have to select the open-source tab and click on the dropdown pointer and select your app as shown in the image :
  78. 4

    NOTE : Here you will have your own application name that is the name given by you while creating the repository.

  79. When you select your app the fetching process will start as shown in the figure :
  80. learn to implement capture api with phonegap5

  81. After the fetching is complete you will get the following window :
  82. 6

  83. Now click on the Ready to Build button as shown by the red arrow in the image and you will get the following screen :
  84. learn to implement capture api with phonegap7

  85. Click on the android icon and you will have the following window popping up to save the apk file.
  86. learn to implement capture api with phonegap8

  87. Now save your apk file and insert it in your android mobile phone and install it ,you will have the application running on your mobile phone
  88. Thus we have learn to implement capture api with phonegap .

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -