将拍摄的照片缩放到屏幕尺寸

时间:2014-05-13 14:25:42

标签: lua corona

我可以使用以下代码拍照:

local centerX = display.contentCenterX
local centerY = display.contentCenterY

local isXcodeSimulator = "iPhone Simulator" == system.getInfo("model")
if (isXcodeSimulator) then
     local alert = native.showAlert( "Information", "Camera API not available on iOS Simulator.", { "OK"})    
end

local sessionComplete = function(event) 
    local image = event.target

    if image then
        image.x = centerX
        image.y = centerY
        local w = image.width
        local h = image.height
    end
end

local listener = function( event )
    if media.hasSource( media.Camera ) then
        media.capturePhoto( { listener = sessionComplete } )
    else
        native.showAlert("Corona", "Camera not found.")
    end
    return true
end

trigger:addEventListener( "tap", listener )

但是如果图片很大,它不会缩小或缩小,我尝试了本地w = display.contentWidth ,但没有发生任何事情。如何将照片缩放到精确的屏幕尺寸,可能需要在Y轴上缩放以填充屏幕

2 个答案:

答案 0 :(得分:4)

如果您只想缩小图片,可以采用两种方法。

1:在显示对象上使用比例(http://docs.coronalabs.com/api/type/DisplayObject/scale.html

object:scale(xScale, yScale)

或我怀疑你正在寻找的第二个解决方案

2:直接设置图像尺寸。

使用object.width和object.height设置图像宽度/高度,并设置显示对象的相应contentWidth和ContentHeight。

将sessionComplete函数更改为以下

local sessionComplete = function(event) 
     local image = event.target
     if image then
         image.x = centerX
         image.y = centerY
         image.width = display.contentWidth
         image.height = display.contentHeight
     end
end

答案 1 :(得分:0)

使用此等式解决了问题

  

image.contentWidth / image.contentHeight * display.contentHeight

local sessionComplete = function(event) 
     local image = event.target
     if image then
     imgWidth = image.contentWidth / image.contentHeight * display.contentHeight 
         image.x = centerX
         image.y = centerY
         image.width = imgWidth
         image.height = display.contentHeight
     end
end