1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CoronaSDKでキャプチャ画像を縮小してファイルに保存する

Posted at

CoronaSDKにて特定のオブジェクトをキャプチャしてサムネイルにしたかったのですが、なかなか上手く画像をリサイズできなくて苦戦しました。正しいやり方かどうかわかりませんが、それなりに動いたので以下にメモとして共有しておきます。

Corona SDK / version : 2016.2949 (2016.9.15)

test.lua
-- 画像の縮小&保存処理
local function saveImage(canvas, newFileName, width, height)
   local resizeCanvas = display.capture(canvas)

   -- ポジションを移動しないと縮小処理が上手くいかない
   resizeCanvas.x = display.actualContentWidth * 0.5 
   resizeCanvas.y = display.actualContentHeight * 0.5

   -- 誤差によりぴったり指定サイズにならないが、近いサイズに縮小
   resizeCanvas:scale(width * display.contentScaleX / canvas.width,
					  height * display.contentScaleY / canvas.height)

   -- 保存処理
   local baseDir = system.DocumentsDirectory
   display.save( resizeCanvas, newFileName, baseDir )
   resizeCanvas:removeSelf()
   resizeCanvas = nil
end

-- 
mainCanvas = display.newGroup()
mainCanvax.insert(...)

...

saveImage(mainCanvas, "tmp.png", 160, 240)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?