Skip to content

Commit

Permalink
Fixed #9
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Oct 15, 2023
1 parent 66623da commit c958b25
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,24 +291,31 @@ else if( orientation == ExifInterface.ORIENTATION_TRANSVERSE )
public static String GetVideoProperties( Context context, final String path )
{
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
metadataRetriever.setDataSource( path );

String width = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH );
String height = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT );
String duration = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_DURATION );
String rotation = "0";
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 )
rotation = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION );

if( width == null )
width = "0";
if( height == null )
height = "0";
if( duration == null )
duration = "0";
if( rotation == null )
rotation = "0";

return width + ">" + height + ">" + duration + ">" + rotation;
try
{
metadataRetriever.setDataSource( path );

String width = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH );
String height = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT );
String duration = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_DURATION );
String rotation = "0";
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 )
rotation = metadataRetriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION );

if( width == null )
width = "0";
if( height == null )
height = "0";
if( duration == null )
duration = "0";
if( rotation == null )
rotation = "0";

return width + ">" + height + ">" + duration + ">" + rotation;
}
finally
{
metadataRetriever.release();
}
}
}
}
Binary file modified Plugins/TextureOps/Android/TextureOps.aar
Binary file not shown.
50 changes: 4 additions & 46 deletions Plugins/TextureOps/README.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
= Texture Ops (v1.2.2) =
= Texture Ops (v1.2.3) =

Online documentation & example code available at: https://github.com/yasirkula/UnityTextureOps
E-mail: [email protected]


1. ABOUT
This plugin helps you save/load textures and perform simple operations on them, like scale and slice.


2. iOS SETUP
iOS setup is normally handled automatically via a post processor script but, if you wish, you can execute the following steps to set up the plugin manually:

- set the value of 'Automated Setup' to false at 'Project Settings/yasirkula/Texture Ops'
- build your project
- insert "-framework MobileCoreServices -framework ImageIO" to the "Other Linker Flags" of Unity-iPhone Target (and UnityFramework Target on Unity 2019.3 or newer)


3. SCRIPTING API
Please see the online documentation for a more in-depth documentation of the Scripting API: https://github.com/yasirkula/UnityTextureOps

//// SAVE-LOAD FUNCTIONS ////
bool TextureOps.SaveImage( Texture2D sourceTex, string imagePath ): saves sourceTex as an image file. If imagePath ends with '.jpeg' or '.jpg', texture will be saved as JPEG, otherwise it will be saved as PNG. Returns true, if the image is saved successfully.
bool TextureOps.SaveImage( byte[] sourceBytes, string imagePath ): saves raw image bytes as an image file. Returns true, if the image is saved successfully.

// format: specifies the format of the texture; RGB24 consumes less memory than RGBA32 but doesn't support transparency
// options: determines the properties of the generated Texture2D. It has the following variables:
// - bool generateMipmaps: determines whether or not the texture will have mipmaps enabled
// - bool linearColorSpace: determines whether the texture will use linear color space or gamma color space. Gamma space is the Unity-specified default value
// - bool markNonReadable: determines whether or not the texture will be marked as non-readable. Such textures consume less memory but don't support read/write operations such as GetPixels/SetPixels and have slower TextureOps.SaveImage performance
Texture2D TextureOps.LoadImage( byte[] imageBytes, TextureFormat format = TextureFormat.RGBA32, TextureOps.Options options ): loads raw image bytes into a Texture2D and returns it.

// maxSize: determines the maximum size of the returned Texture2D in pixels. Larger textures will be down-scaled. If untouched, its value will be set to SystemInfo.maxTextureSize. It is recommended to set a proper maxSize for better performance
Texture2D TextureOps.LoadImage( string imagePath, int maxSize = -1, TextureOps.Options options ): creates a Texture2D from the specified image file and returns it.
async Task<Texture2D> TextureOps.LoadImageAsync( string imagePath, int maxSize = -1, TextureOps.Options options ): creates a Texture2D from the specified image file and returns it.

//// TEXTURE OPERATIONS ////
// NOTE: on some Android devices, these functions may not work correctly when called with a 'sourceTex' that was created in the same frame. Therefore, if you'd like to call these functions immediately after 'LoadImage', consider instead waiting for at least one frame. You can use `yield return null;` in a coroutine to wait for one frame.

Texture2D TextureOps.Crop( Texture2D, sourceTex, int leftOffset, int topOffset, int width, int height, TextureFormat format = TextureFormat.RGBA32, TextureOps.Options options ): crops sourceTex and returns the cropped texture.
Texture2D TextureOps.Scale( Texture2D sourceTex, int targetWidth, int targetHeight, TextureFormat format = TextureFormat.RGBA32, TextureOps.Options options ): scales sourceTex to the specified size and returns the scaled texture. sourceTex's aspect ratio may not be preserved.
Texture2D TextureOps.ScaleFill( Texture2D sourceTex, int targetWidth, int targetHeight, Color32 fillColor, TextureFormat format = TextureFormat.RGBA32, TextureOps.Options options ): scales sourceTex to the specified size and returns the scaled texture. sourceTex's aspect ratio is preserved and blank space is filled with fillColor.
Texture2D[] TextureOps.Slice( Texture2D sourceTex, int sliceTexWidth, int sliceTexHeight, TextureFormat format = TextureFormat.RGBA32, TextureOps.Options options ): slices sourceTex into smaller textures and returns these textures in an array. sourceTex is sliced row-by-row, starting from top-left corner.

//// UTILITY FUNCTIONS ////
TextureOps.ImageProperties TextureOps.GetImageProperties( string imagePath ): [Android & iOS only] returns an ImageProperties instance that holds the width, height, mime type and EXIF orientation information of an image file without creating a Texture2D object. Mime type will be null, if it can't be determined.
TextureOps.VideoProperties TextureOps.GetVideoProperties( string videoPath ): [Android & iOS only] returns a VideoProperties instance that holds the width, height, duration (in milliseconds) and rotation information of a video file.
Documentation: https://github.com/yasirkula/UnityTextureOps
Example code: https://github.com/yasirkula/UnityTextureOps#example-code
E-mail: [email protected]
5 changes: 4 additions & 1 deletion Plugins/TextureOps/TextureOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ public static bool SaveImage( byte[] sourceBytes, string imagePath )

try
{
Directory.CreateDirectory( Path.GetDirectoryName( imagePath ) );
string parentDirectoryPath = Path.GetDirectoryName( imagePath );
if( !Directory.Exists( parentDirectoryPath ) )
Directory.CreateDirectory( parentDirectoryPath );

File.WriteAllBytes( imagePath, sourceBytes );
}
catch( Exception e )
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.textureops",
"displayName": "Texture Ops",
"version": "1.2.2",
"version": "1.2.3",
"documentationUrl": "https://github.com/yasirkula/UnityTextureOps",
"changelogUrl": "https://github.com/yasirkula/UnityTextureOps/releases",
"licensesUrl": "https://github.com/yasirkula/UnityTextureOps/blob/master/LICENSE.txt",
Expand Down

0 comments on commit c958b25

Please sign in to comment.