Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Simple example to use camera on webview
Started by luisleitaoaudio Jul 18 2018 01:13 PM

No replies to this topic
webview post camera capture
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

luisleitaoaudio

[GLOBAL: userInfoPane.html]
luisleitaoaudio
  • Observer

  • 1 posts
  • Corona SDK

Hello, I'm  php developer, and I'm trying to test the coronalabs, very simple: D very cool: D
I need to capture picture from camera inside webview.
So I search and I can not find solution ... so I will share my solution: D
We created a url to capture, we captured the photo and we sent via post ...
I'm still at the beginning: D now just use your imagination.
I am a developer for many years but new here with coronalabs: D
I hope to help someone !!
 
 
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here
local MultipartFormData = require("class_MultipartFormData")
--local notifications = require( "plugin.notifications.v2" )



local function webListener( event )
    if event.url then
		if event.url  == "http://cook.dapicart.com/addreceita?getfoto" then

				if media.hasSource( media.Camera ) then
					            media.capturePhoto({listener=captured, destination = {baseDir = system.TemporaryDirectory, filename = "myfile.jpg", type = "image"}})
								local path = system.pathForFile("myfile.jpg", system.TemporaryDirectory)						
								local multipart = MultipartFormData.new()
								--multipart:addHeader("Customer-Header", "Custom Header Value")
								multipart:addField("addpictureviaapp","true")
								multipart:addFile("myfile", path, "image/jpeg", "myfile.jpg")
								local params = {}
								params.body = multipart:getBody() -- Must call getBody() first!
								params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called.
								local function networkListener22( event )
									if ( event.isError ) then
										print( "Network error!")
									else
										native.showAlert( "Corona",  event.response, { "OK" } )
										print ( "RESPONSE: " .. event.response )
									end
								end
								
								
								
								network.request( "http://cook.dapicart.com/addreceita", "POST", networkListener22, params)			
								
				else
					native.showAlert( "Corona", "This device does not have a camera.", { "OK" } )
				end
				
				
		end
		print( "You are visiting: " .. event.url )
    end
  
    if event.type then
        print( "The event.type is " .. event.type ) -- print the type of request
    end
  
    if event.errorCode then
        native.showAlert( "Error!", event.errorMessage, { "OK" } )
    end
end
 

  
local webView = native.newWebView(display.contentCenterX , display.contentCenterY, display.contentWidth, display.contentHeight )
webView:request( "http://cook.dapicart.com/?app=yes" )
webView:addEventListener( "urlRequest", webListener )

A little of PHP code 

move_uploaded_file($_FILES['myfile']['tmp_name'],dirname(__FILE__)."/myfile.jpg");
  $this->base64file_decode( dirname(__FILE__)."/myfile.jpg", dirname(__FILE__)."/myfile.jpg" );

 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: webview, post, camera, capture