Difference between revisions of "Media"

From GiderosMobile
m (added one more example)
(formatting)
Line 4: Line 4:
  
 
<!-- GIDEROSOBJ:Media -->
 
<!-- GIDEROSOBJ:Media -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]
+
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/>
<br/>
 
  
'''<translate>Available since</translate>:''' Gideros 2016.1
+
'''<translate>Available since</translate>:''' Gideros 2016.1<br/>
<br/>
 
  
 
=== <translate>Description</translate> ===
 
=== <translate>Description</translate> ===
<translate>
 
 
* Get image from Camera, gallery or file system
 
* Get image from Camera, gallery or file system
 
* Resize image
 
* Resize image
Line 19: Line 16:
 
* Manipulate pictures (getting/setting pixels)
 
* Manipulate pictures (getting/setting pixels)
 
* And much more...
 
* And much more...
</translate>
 
  
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
 
'''Example 1.'''
 
'''Example 1.'''
<br/>
 
  
 
<source lang="lua">
 
<source lang="lua">
Line 41: Line 36:
 
mediamanager:postPicture("gfx/ball.png")
 
mediamanager:postPicture("gfx/ball.png")
 
</source>
 
</source>
<br/>
 
  
  
 
'''Example 2.'''
 
'''Example 2.'''
<br/>
 
  
 
<source lang="lua">
 
<source lang="lua">
Line 69: Line 62:
 
end
 
end
 
</source>
 
</source>
<br/>
 
  
  
 
'''Example 3.''' Save a picture on your phone or on your pc.
 
'''Example 3.''' Save a picture on your phone or on your pc.
<br/>
 
  
 
<source lang="lua">
 
<source lang="lua">
Line 96: Line 87:
 
stage:addChild(mybmprt)
 
stage:addChild(mybmprt)
 
</source>
 
</source>
<br/>
 
 
  
 
=== <translate>Notes</translate> ===
 
=== <translate>Notes</translate> ===

Revision as of 15:04, 8 December 2019



Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png

Available since: Gideros 2016.1

Description

  • Get image from Camera, gallery or file system
  • Resize image
  • Make copy of an image
  • Take screenshots
  • Play videos (for cutscenes)
  • Manipulate pictures (getting/setting pixels)
  • And much more...

Examples

Example 1.

--require plugin
require "media"

-- is camera available
print(mediamanager:isCameraAvailable())

-- take screenshot (no permission needed)
mediamanager:takeScreenshot()

-- get picture from gallery (*need read permission*)
mediamanager:getPicture()

-- save picture to sdcard/pictures folder (*need write permission*)
mediamanager:postPicture("gfx/ball.png")


Example 2.

require "media"
mediamanager:addEventListener(Event.MEDIA_RECEIVE, function(e)
    local media = Media.new(e.path)
    media:resize(200, 200)
    print(media:getPixel(1, 1))
    for x = 50, 100 do
        for y = 50, 100 do
            media:setPixel(x, y, 255, 0, 0, 0.5)
        end
    end
    media:save()
    local bmp = Bitmap.new(Texture.new(e.path, true))
    stage:addChild(bmp)
end)

if mediamanager:isCameraAvailable() then
    mediamanager:takePicture()
else
    mediamanager:getPicture()
end


Example 3. Save a picture on your phone or on your pc.

require "media"
 
-- draw your gfx
local source = Pixel.new(0xff00ff, 1, 128, 128)
-- create a render target and draw to it
local rt = RenderTarget.new(source:getWidth(), source:getHeight())
rt:draw(source)
-- save your render target to gideros documents folder
local myfilepath = "|D|mysavedpicture2.png"
rt:save(myfilepath)
-- create a new media and save your gfx
local media = Media.new(myfilepath)
-- android saved path = internal storage/pictures !!! NEED permission write external storage !!!
mediamanager:postPicture(myfilepath)
 
-- show your gfx on stage
local mybmprt = Bitmap.new(Texture.new(myfilepath))
mybmprt:setPosition(64, 64)
stage:addChild(mybmprt)

Notes

By resizing image you will actually resize the file of the image, so there is no going back to upscale it later.
getPicture method gives you copies of the image, so you won't harm the original.
The dimensions when resizing are all actual dimensions of the image and not logical dimensions in your project.

Methods

mediamanager:isCameraAvailable() -- returns true is you can get picture from camera
mediamanager:takePicture() -- allow user to provide picture input from camera
mediamanager:getPicture() -- allow user to select picture from gallery
mediamanager:takeScreenshot() -- capture the screenshot of the app
mediamanager:postPicture(path) -- add picture from given path to gallery (or open save file dialog on pc)
mediamanager:playVideo(path, force) -- play video file in given path (bool force to watch till the end, or quit playing on tap)
mediamanager:deleteFile(path) -- deletes file at provided path

Media.new(path) -- create new Media object with provided image
Media.new(width, height) -- create blank Media object with provided dimensions
Media:getPath() -- returns the path that was used to create object
Media:getWidth() -- returns the width of the image
Media:getHeight() -- returns the height of the image
Media:getPixel(x, y) -- returns r, g, b, a values of pixel at x,y coordinate (RGB are in range of 0 to 255, alpha is in range of 0 to 1)
Media:setPixel(x, y, r, g, b, a) -- sets r, g, b, a values to pixel at x,y coordinate (RGB are in range of 0 to 255, alpha is in range of 0 to 1)
Media:setPixel(x, y, hex, a, blendAlpha) -- sets hex, a values to pixel at x,y coordinate (alpha is in range of 0 to 1)
Media:resizeWidth(newWidth, fixed) -- resize image width, if fixed is true image is resized with fixed aspect ratio
Media:resizeHeight(newHeight, fixed) -- resize image height, if fixed is true image is resized with fixed aspect ratio
Media:resize(newWidth, newHeight, fixed, crop) -- resize image to new width and new height, if fixed is true image is resized with fixed aspect ratio and takes crop value into account (by either cropping image if true, or doing letterbox resize if false)
Media:crop(x, y, width, height) -- crop with and height from x and y point from a given image
Media:copy(destination) -- make a copy of the image
Media:save() -- save the changes made to file
Media:setRotation() -- rotate image to provided angle in degrees
Media:getRotation() -- return previously set rotation
Media:getRotation() -- return previously set rotation
Media:flipHorizontal() -- flip image horizontally
Media:flipVertical() -- flip image vertically
Media:drawImage(x, y, media, alpha) -- draw another image on current one at specific x and y coordinates. You can provide either another media object or string path to image file
Media:drawText(x, y, text, hexColor, fontSize, alpha) -- draw a text at provided coorindates with provided settings
Media:drawLine(x0, y0, x1, y0, hexColor, alpha, blendAlpha) -- draw a line from x0,y0 to x1,y1 with provided settings
Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha) -- draw filled rectangle at x,y position with width,height dimensions and with provided settings
Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha) -- fill area with provided color based on tolerance to other colors
Media:trim(hexColor) -- trim image by provided background color, if color is not provided, uses top left pixel to crop

Events

Event.MEDIA_RECEIVE (e.path) -- path to image
Event.MEDIA_CANCEL --user cancelled media input
Event.VIDEO_COMPLETE --user completed watching video

Constants