Difference between revisions of "Media"

From GiderosMobile
Line 112: Line 112:
 
<!-- GIDEROSMTD:Media.new(width, height) creates a blank Media object with provided dimensions -->
 
<!-- GIDEROSMTD:Media.new(width, height) creates a blank Media object with provided dimensions -->
  
 +
[[Special:MyLanguage/x_Media:copy|Media:copy(destination)]] ''makes a copy of an image''<br/>
 +
<!-- GIDEROSMTD:Media:copy(destination) makes a copy of an image -->
 +
[[Special:MyLanguage/x_Media:crop|Media:crop(x, y, width, height)]] ''crops an image''<br/>
 +
<!-- GIDEROSMTD:Media:crop(x, y, width, height) crops an image width and height from x and y point -->
 +
[[Special:MyLanguage/x_Media:drawFill|Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha)]] ''draws a filled rectangle on current image''<br/>
 +
<!-- GIDEROSMTD:Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha) draws a filled rectangle at x,y position with width,height dimensions and provided settings-->
 +
[[Special:MyLanguage/x_Media:drawImage|Media:drawImage(x, y, media, alpha)]] ''draws another image on current one''<br/>
 +
<!-- GIDEROSMTD:Media:drawImage(x, y, media, alpha) draws another image on current one at specific x and y coordinates. You can provide either another media object or string path to image file-->
 +
[[Special:MyLanguage/x_Media:drawLine|Media:drawLine(x0, y0, x1, y1, hexColor, alpha, blendAlpha)]] ''draws a line on current image''<br/>
 +
<!-- GIDEROSMTD:Media:drawLine(x0, y0, x1, y1, hexColor, alpha, blendAlpha) draws a line from x0,y0 to x1,y1 with provided settings-->
 +
[[Special:MyLanguage/x_Media:drawText|Media:drawText(x, y, text, hexColor, fontSize, alpha)]] ''draws a text on current image''<br/>
 +
<!-- GIDEROSMTD:Media:drawText(x, y, text, hexColor, fontSize, alpha) draws a text at x,y coordinates with provided settings-->
 +
[[Special:MyLanguage/x_Media:flipHorizontal|Media:flipHorizontal()]] ''flips image horizontally''<br/>
 +
<!-- GIDEROSMTD:Media:flipHorizontal() flips image horizontally -->
 +
[[Special:MyLanguage/x_Media:flipVertical|Media:flipVertical()]] ''flips image vertically''<br/>
 +
<!-- GIDEROSMTD:Media:flipVertical() flips image vertically-->
 +
[[Special:MyLanguage/x_Media:floodFill|Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha)]] ''flood fills current image''<br/>
 +
<!-- GIDEROSMTD:Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha) flood fills area with provided color based on tolerance to other colors-->
 +
[[Special:MyLanguage/x_Media:getHeight|Media:getHeight()]] ''returns the height of the image''<br/>
 +
<!-- GIDEROSMTD:Media:getHeight() returns the height of the image -->
 
[[Special:MyLanguage/x_Media:getPath|Media:getPath()]] ''returns the path that was used to create the media''<br/>
 
[[Special:MyLanguage/x_Media:getPath|Media:getPath()]] ''returns the path that was used to create the media''<br/>
 
<!-- GIDEROSMTD:Media:getPath() returns the path that was used to create the media -->
 
<!-- GIDEROSMTD:Media:getPath() returns the path that was used to create the media -->
[[Special:MyLanguage/x_Media:getHeight|Media:getHeight()]] ''returns the height of the image''<br/>
+
[[Special:MyLanguage/x_Media:getPixel|Media:getPixel(x, y)]] ''returns r, g, b, a values of pixel at x,y coordinate''<br/>
<!-- GIDEROSMTD:Media:getHeight() returns the height of the image -->
+
<!-- GIDEROSMTD: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) -->
 +
[[Special:MyLanguage/x_Media:getRotation|Media:getRotation()]] ''returns current rotation''<br/>
 +
<!-- GIDEROSMTD:Media:getRotation() returns previously set rotation -->
 
[[Special:MyLanguage/x_Media:getWidth|Media:getWidth()]] ''returns the width of the image''<br/>
 
[[Special:MyLanguage/x_Media:getWidth|Media:getWidth()]] ''returns the width of the image''<br/>
 
<!-- GIDEROSMTD:Media:getWidth() returns the width of the image -->
 
<!-- GIDEROSMTD:Media:getWidth() returns the width of the image -->
[[Special:MyLanguage/x_Media:getPixel|Media:getPixel(x, y)]] ''returns r, g, b, a values of pixel at x,y coordinate''<br/>
+
[[Special:MyLanguage/x_Media:resize|Media:resize(newWidth, newHeight, fixed, crop)]] ''resizes image width and height''<br/>
<!-- GIDEROSMTD: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) -->
+
<!-- GIDEROSMTD:Media:resize(newWidth, newHeight, fixed, crop) resizes image width and height, if fixed is true image is resized with fixed aspect ratio and takes crop value into account (cropping image if true or letterbox resize if false) -->
 +
[[Special:MyLanguage/x_Media:resizeHeight|Media:resizeHeight(newHeight, fixed)]] ''resizes image height''<br/>
 +
<!-- GIDEROSMTD:Media:resizeHeight(newHeight, fixed) resizes image height, if fixed is true image is resized with fixed aspect ratio -->
 +
[[Special:MyLanguage/x_Media:resizeWidth|Media:resizeWidth(newWidth, fixed)]] ''resizes image width''<br/>
 +
<!-- GIDEROSMTD:Media:resizeWidth(newWidth, fixed) resizes image width, if fixed is true image is resized with fixed aspect ratio -->
 +
[[Special:MyLanguage/x_Media:save|Media:save()]] ''saves the changes made to file''<br/>
 +
<!-- GIDEROSMTD:Media:save() saves the changes made to file -->
 
[[Special:MyLanguage/x_Media:setPixel|Media:setPixel(x, y, r, g, b, a)]] ''sets r, g, b, a values to pixel at x,y coordinate''<br/>
 
[[Special:MyLanguage/x_Media:setPixel|Media:setPixel(x, y, r, g, b, a)]] ''sets r, g, b, a values to pixel at x,y coordinate''<br/>
 
<!-- GIDEROSMTD: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) -->
 
<!-- GIDEROSMTD: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) -->
 
[[Special:MyLanguage/x_Media:setPixel|Media:setPixel(x, y, hex, a, blendAlpha)]] ''sets hex color and alpha values to pixel at x,y coordinate''<br/>
 
[[Special:MyLanguage/x_Media:setPixel|Media:setPixel(x, y, hex, a, blendAlpha)]] ''sets hex color and alpha values to pixel at x,y coordinate''<br/>
 
<!-- GIDEROSMTD:Media:setPixel(x, y, hex, a, blendAlpha) sets hex color and alpha values to pixel at x,y coordinate (alpha is in range of 0 to 1) -->
 
<!-- GIDEROSMTD:Media:setPixel(x, y, hex, a, blendAlpha) sets hex color and alpha values to pixel at x,y coordinate (alpha is in range of 0 to 1) -->
[[Special:MyLanguage/x_Media:resizeWidth|Media:resizeWidth(newWidth, fixed)]] ''resizes image width''<br/>
 
<!-- GIDEROSMTD:Media:resizeWidth(newWidth, fixed) resizes image width, if fixed is true image is resized with fixed aspect ratio -->
 
[[Special:MyLanguage/x_Media:resizeHeight|Media:resizeHeight(newHeight, fixed)]] ''resizes image height''<br/>
 
<!-- GIDEROSMTD:Media:resizeHeight(newHeight, fixed) resizes image height, if fixed is true image is resized with fixed aspect ratio -->
 
[[Special:MyLanguage/x_Media:resize|Media:resize(newWidth, newHeight, fixed, crop)]] ''resizes image width and height''<br/>
 
<!-- GIDEROSMTD:Media:resize(newWidth, newHeight, fixed, crop) resizes image width and height, if fixed is true image is resized with fixed aspect ratio and takes crop value into account (cropping image if true or letterbox resize if false) -->
 
[[Special:MyLanguage/x_Media:crop|Media:crop(x, y, width, height)]] ''crops an image''<br/>
 
<!-- GIDEROSMTD:Media:crop(x, y, width, height) crops an image width and height from x and y point -->
 
[[Special:MyLanguage/x_Media:copy|Media:copy(destination)]] ''makes a copy of an image''<br/>
 
<!-- GIDEROSMTD:Media:copy(destination) makes a copy of an image -->
 
[[Special:MyLanguage/x_Media:save|Media:save()]] ''saves the changes made to file''<br/>
 
<!-- GIDEROSMTD:Media:save() saves the changes made to file -->
 
 
[[Special:MyLanguage/x_Media:setRotation|Media:setRotation(angle)]] ''rotates image''<br/>
 
[[Special:MyLanguage/x_Media:setRotation|Media:setRotation(angle)]] ''rotates image''<br/>
 
<!-- GIDEROSMTD:Media:setRotation(angle) rotates image to provided angle in degrees -->
 
<!-- GIDEROSMTD:Media:setRotation(angle) rotates image to provided angle in degrees -->
[[Special:MyLanguage/x_Media:getRotation|Media:getRotation()]] ''returns current rotation''<br/>
 
<!-- GIDEROSMTD:Media:getRotation() returns previously set rotation -->
 
[[Special:MyLanguage/x_Media:flipHorizontal|Media:flipHorizontal()]] ''flips image horizontally''<br/>
 
<!-- GIDEROSMTD:Media:flipHorizontal() flips image horizontally -->
 
[[Special:MyLanguage/x_Media:flipVertical|Media:flipVertical()]] ''flips image vertically''<br/>
 
<!-- GIDEROSMTD:Media:flipVertical() flips image vertically-->
 
[[Special:MyLanguage/x_Media:drawImage|Media:drawImage(x, y, media, alpha)]] ''draws another image on current one''<br/>
 
<!-- GIDEROSMTD:Media:drawImage(x, y, media, alpha) draws another image on current one at specific x and y coordinates. You can provide either another media object or string path to image file-->
 
 
[[Special:MyLanguage/x_Media:drawText|Media:drawText(x, y, text, hexColor, fontSize, alpha)]] ''draws a text on current image''<br/>
 
<!-- GIDEROSMTD:Media:drawText(x, y, text, hexColor, fontSize, alpha) draws a text at x,y coordinates with provided settings-->
 
 
[[Special:MyLanguage/x_Media:drawLine|Media:drawLine(x0, y0, x1, y0, hexColor, alpha, blendAlpha)]] ''draws a line on current image''<br/>
 
<!-- GIDEROSMTD:Media:drawLine(x0, y0, x1, y0, hexColor, alpha, blendAlpha) draws a line from x0,y0 to x1,y1 with provided settings-->
 
 
[[Special:MyLanguage/x_Media:drawFill|Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha)]] ''draws a filled rectangle on current image''<br/>
 
<!-- GIDEROSMTD:Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha) draws a filled rectangle at x,y position with width,height dimensions and provided settings-->
 
 
[[Special:MyLanguage/x_Media:floodFill|Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha)]] ''flood fills current image''<br/>
 
<!-- GIDEROSMTD:Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha) fills area with provided color based on tolerance to other colors-->
 
 
 
[[Special:MyLanguage/x_Media:trim|Media:trim(hexColor)]] ''trims current image''<br/>
 
[[Special:MyLanguage/x_Media:trim|Media:trim(hexColor)]] ''trims current image''<br/>
 
<!-- GIDEROSMTD:Media:trim(hexColor) trims image by provided background color, if color is not provided, uses top left pixel to crop-->
 
<!-- GIDEROSMTD:Media:trim(hexColor) trims image by provided background color, if color is not provided, uses top left pixel to crop-->

Revision as of 02:38, 11 February 2020


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:deleteFile(path) deletes file at provided path
mediamanager:getPicture selects picture from gallery
mediamanager:isCameraAvailable returns true if you can get picture from camera
mediamanager:playVideo(path, force) plays video file in given path (bool force to watch till the end, or quit playing on tap)
mediamanager:postPicture(path) adds picture from given path to gallery (or opens save file dialog on pc)
mediamanager:takePicture allows user to provide picture input from camera
mediamanager:takeScreenshot captures a screenshot of the app

Media.new(path) creates a new Media object with provided image
Media.new(width, height) creates a blank Media object with provided dimensions

Media:copy(destination) makes a copy of an image
Media:crop(x, y, width, height) crops an image
Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha) draws a filled rectangle on current image
Media:drawImage(x, y, media, alpha) draws another image on current one
Media:drawLine(x0, y0, x1, y1, hexColor, alpha, blendAlpha) draws a line on current image
Media:drawText(x, y, text, hexColor, fontSize, alpha) draws a text on current image
Media:flipHorizontal() flips image horizontally
Media:flipVertical() flips image vertically
Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha) flood fills current image
Media:getHeight() returns the height of the image
Media:getPath() returns the path that was used to create the media
Media:getPixel(x, y) returns r, g, b, a values of pixel at x,y coordinate
Media:getRotation() returns current rotation
Media:getWidth() returns the width of the image
Media:resize(newWidth, newHeight, fixed, crop) resizes image width and height
Media:resizeHeight(newHeight, fixed) resizes image height
Media:resizeWidth(newWidth, fixed) resizes image width
Media:save() saves the changes made to file
Media:setPixel(x, y, r, g, b, a) sets r, g, b, a values to pixel at x,y coordinate
Media:setPixel(x, y, hex, a, blendAlpha) sets hex color and alpha values to pixel at x,y coordinate
Media:setRotation(angle) rotates image
Media:trim(hexColor) trims current image

Events

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

Constants