Difference between revisions of "Media"

From GiderosMobile
(added examples)
m
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
<!-- GIDEROSOBJ:Media -->
 +
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/>
 +
'''Available since:''' Gideros 2016.1<br/>
  
<languages />
+
=== Description ===
 +
'''THIS PLUGIN IS NOT AVAILABLE FOR WINDOWS 32 BITS :-('''
  
<!-- GIDEROSOBJ:Media -->
+
'''The Media functions wiki pages are prefixed by X_ because ''media'' is a reserved wiki word'''
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]
 
<br/>
 
  
'''<translate>Available since</translate>:''' Gideros ?
+
* Get/save files from file system (desktop only)
<br/>
+
* Get image from Camera, gallery or file system (mobile and desktop)
 
 
=== <translate>Description</translate> ===
 
<translate>
 
* Get image from Camera, gallery or file system
 
 
* Resize image
 
* Resize image
 
* Make copy of an image
 
* Make copy of an image
 
* Take screenshots  
 
* Take screenshots  
 
* Play videos (for cutscenes)
 
* Play videos (for cutscenes)
* Manipulate pictures (getting/setting pixels)
+
* Manipulate pictures (getting/setting pixels, flood fill)
 
* And much more...
 
* And much more...
</translate>
 
  
=== <translate>Examples</translate> ===
+
<syntaxhighlight lang="lua">
<source lang="lua">
+
require "media"
-- is camera available
+
</syntaxhighlight>
print(mediamanager:isCameraAvailable())
 
  
-- take screenshot (no permission needed)
+
===='''[[Media_Plugin|Here is an extensive example]]'''====
mediamanager:takeScreenshot()
 
  
-- get picture from gallery (*need read permission*)
+
=== '''Notes''' ===
mediamanager:getPicture()
+
By resizing image you will actually resize the file of the image, so there is no going back to upscale it later.
  
-- save picture to sdcard/pictures folder (*need write permission*)
+
''getPicture'' method gives you copies of the image, so you won't harm the original.
mediamanager:postPicture("gfx/ball.png")
 
</source>
 
  
=== <translate>Notes</translate> ===
 
By resizing image you will actually resize the file of the image, so there is no going back to upscale it later.
 
<br/>
 
getPicture method gives you copies of the image, so you won't harm the original.
 
<br/>
 
 
The dimensions when resizing are all actual dimensions of the image and not logical dimensions in your project.
 
The dimensions when resizing are all actual dimensions of the image and not logical dimensions in your project.
<br/>
+
 
<br/>
+
=== Example ===
 +
'''Saving a picture on your phone or on your PC'''
 +
<syntaxhighlight lang="lua">
 +
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)
 +
</syntaxhighlight>
  
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
'''mediamanager:isCameraAvailable()''' -- returns true is you can get picture from camera<br/>
+
[[mediamanager:deleteFile]] ''deletes file at provided path''<br/><!--GIDEROSMTD:mediamanager:deleteFile(path) deletes file at provided path-->
'''mediamanager:takePicture()''' -- allow user to provide picture input from camera<br/>
+
[[mediamanager:getFile]] ''selects file from file system''<br/><!--GIDEROSMTD:mediamanager:getFile(extensions, initialPath) selects file from file system-->
'''mediamanager:getPicture()''' -- allow user to select picture from gallery<br/>
+
[[mediamanager:getPicture]] ''selects picture from gallery''<br/><!--GIDEROSMTD:mediamanager:getPicture() selects picture from gallery-->
'''mediamanager:takeScreenshot()''' -- capture the screenshot of the app<br/>
+
[[mediamanager:isCameraAvailable]] ''returns true if you can get picture from camera''<br/><!--GIDEROSMTD:mediamanager:isCameraAvailable() returns true if you can get picture from camera-->
'''mediamanager:postPicture(path)''' -- add picture from given path to gallery (or open save file dialog on pc)<br/>
+
[[mediamanager:playVideo]] ''plays video file in given path (bool force to watch till the end, or quit playing on tap)''<br/><!--GIDEROSMTD:mediamanager:playVideo(path, force) plays video file in given path (bool force to watch till the end, or quit playing on tap)-->
'''mediamanager:playVideo(path, force)''' -- play video file in given path (bool force to watch till the end, or quit playing on tap)<br/>
+
[[mediamanager:postPicture]] ''adds picture from given path to gallery (or opens save file dialog on pc)''<br/><!--GIDEROSMTD:mediamanager:postPicture(path) adds picture from given path to gallery (or opens save file dialog on pc)-->
'''mediamanager:deleteFile(path)''' -- deletes file at provided path<br/>
+
[[mediamanager:saveFile]] ''saves file to file system''<br/><!--GIDEROSMTD:mediamanager:saveFile(path, initialPath) saves file to file system-->
<br/>
+
[[mediamanager:takePicture]] ''allows user to provide picture input from camera''<br/><!--GIDEROSMTD:mediamanager:takePicture() allows user to provide picture input from camera-->
 +
[[mediamanager:takeScreenshot]] ''captures a screenshot of the app''<br/><!--GIDEROSMTD:mediamanager:takeScreenshot() captures a screenshot of the app-->
  
'''Media.new(path)''' -- create new Media object with provided image<br/>
+
[[Media.new]] ''creates a new Media object with provided image''<br/><!--GIDEROSMTD:Media.new(path) creates a new Media object with provided image-->
'''Media.new(width, height)''' -- create blank Media object with provided dimensions<br/>
+
[[Media.new]] ''creates a blank Media object with provided dimensions''<br/><!--GIDEROSMTD:Media.new(width, height) creates a blank Media object with provided dimensions-->
'''Media:getPath()''' -- returns the path that was used to create object<br/>
+
 
'''Media:getWidth()''' -- returns the width of the image<br/>
+
[[x_Media:copy|Media:copy(destination)]] ''makes a copy of an image''<br/><!--GIDEROSMTD:Media:copy(destination) makes a copy of an image-->
'''Media:getHeight()''' -- returns the height of the image<br/>
+
[[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-->
'''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)<br/>
+
[[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-->
'''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)<br/>
+
[[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-->
'''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)<br/>
+
[[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-->
'''Media:resizeWidth(newWidth, fixed)''' -- resize image width, if fixed is true image is resized with fixed aspect ratio<br/>
+
[[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-->
'''Media:resizeHeight(newHeight, fixed)''' -- resize image height, if fixed is true image is resized with fixed aspect ratio<br/>
+
[[x_Media:flipHorizontal|Media:flipHorizontal()]] ''flips image horizontally''<br/><!--GIDEROSMTD:Media:flipHorizontal() flips image horizontally-->
'''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)<br/>
+
[[x_Media:flipVertical|Media:flipVertical()]] ''flips image vertically''<br/><!--GIDEROSMTD:Media:flipVertical() flips image vertically-->
'''Media:crop(x, y, width, height)''' -- crop with and height from x and y point from a given image<br/>
+
[[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-->
'''Media:copy(destination)''' -- make a copy of the image<br/>
+
[[x_Media:getHeight|Media:getHeight()]] ''returns the height of the image''<br/><!--GIDEROSMTD:Media:getHeight() returns the height of the image-->
'''Media:save()''' -- save the changes made to file<br/>
+
[[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-->
'''Media:setRotation()''' -- rotate image to provided angle in degrees<br/>
+
[[x_Media:getPixel|Media:getPixel(x, y)]] ''returns r, g, b, a values of pixel at x,y coordinate''<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)-->
'''Media:getRotation()''' -- return previously set rotation<br/>
+
[[x_Media:getRotation|Media:getRotation()]] ''returns current rotation''<br/><!--GIDEROSMTD:Media:getRotation() returns previously set rotation-->
'''Media:getRotation()''' -- return previously set rotation<br/>
+
[[x_Media:getWidth|Media:getWidth()]] ''returns the width of the image''<br/><!--GIDEROSMTD:Media:getWidth() returns the width of the image-->
'''Media:flipHorizontal()''' -- flip image horizontally<br/>
+
[[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)-->
'''Media:flipVertical()''' -- flip image vertically<br/>
+
[[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-->
'''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<br/>
+
[[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-->
'''Media:drawText(x, y, text, hexColor, fontSize, alpha)''' -- draw a text at provided coorindates with provided settings<br/>
+
[[x_Media:save|Media:save()]] ''saves the changes made to file''<br/><!--GIDEROSMTD:Media:save() saves the changes made to file-->
'''Media:drawLine(x0, y0, x1, y0, hexColor, alpha, blendAlpha)''' -- draw a line from x0,y0 to x1,y1 with provided settings<br/>
+
[[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)-->
'''Media:drawFill(x, y, width, height, hexColor, alpha, blendAlpha)''' -- draw filled rectangle at x,y position with width,height dimensions and with provided settings<br/>
+
[[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)-->
'''Media:floodFill(x, y, hexColor, alpha, tolerance, blendAlpha)''' -- fill area with provided color based on tolerance to other colors<br/>
+
[[x_Media:setRotation|Media:setRotation(angle)]] ''rotates image''<br/><!--GIDEROSMTD:Media:setRotation(angle) rotates image to provided angle in degrees-->
'''Media:trim(hexColor)''' -- trim image by provided background color, if color is not provided, uses top left pixel to crop<br/>
+
[[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-->
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 +
=== Events ===
 +
[[Event.MEDIA_RECEIVE]] ''(e.path) path to image''<br/><!--Event.MEDIA_RECEIVE mediaReceive-->
 +
[[Event.MEDIA_CANCEL]] ''user cancelled media input''<br/><!--Event.MEDIA_CANCEL mediaCancel-->
 +
[[Event.VIDEO_COMPLETE]] ''user completed watching video''<br/><!--Event.VIDEO_COMPLETE videoComplete-->
  
=== <translate>Events</translate> ===
+
=== Constants ===
[[Special:MyLanguage/Event.MEDIA_RECEIVE|Event.MEDIA_RECEIVE]] ''(e.path) -- path to image''<br/><!-- Event.MEDIA_RECEIVE mediaReceive-->
+
|}
[[Special:MyLanguage/Event.MEDIA_CANCEL|Event.MEDIA_CANCEL]] ''--user canceled media input''<br/><!-- Event.MEDIA_CANCEL mediaCancel-->
 
[[Special:MyLanguage/Event.VIDEO_COMPLETE|Event.VIDEO_COMPLETE]] ''--user completed watching video''<br/><!-- Event.VIDEO_COMPLETE videoComplete-->
 
  
=== <translate>Constants</translate> ===
+
{{GIDEROS IMPORTANT LINKS}}
 
 
|}
 

Latest revision as of 23:31, 16 October 2024

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png
Available since: Gideros 2016.1

Description

THIS PLUGIN IS NOT AVAILABLE FOR WINDOWS 32 BITS :-(
The Media functions wiki pages are prefixed by X_ because media is a reserved wiki word
  • Get/save files from file system (desktop only)
  • Get image from Camera, gallery or file system (mobile and desktop)
  • Resize image
  • Make copy of an image
  • Take screenshots
  • Play videos (for cutscenes)
  • Manipulate pictures (getting/setting pixels, flood fill)
  • And much more...
require "media"

Here is an extensive example

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.

Example

Saving 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)

Methods

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

Media.new creates a new Media object with provided image
Media.new 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