From: hsgwon Date: Wed, 10 Jul 2019 08:25:41 +0000 (+0900) Subject: [ImageUtil] Add sync thumbnail extract API (#928) X-Git-Tag: submit/tizen/20190711.005159~1^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=345244cf7a66f9343db88f62755ec5193a27f1d1;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [ImageUtil] Add sync thumbnail extract API (#928) * [ImageUtil] Add sync thumbnail extract API --- diff --git a/src/Tizen.Multimedia.Util/Interop/Interop.ThumbnailExtractor.cs b/src/Tizen.Multimedia.Util/Interop/Interop.ThumbnailExtractor.cs index c1a02de2c..49b298ccb 100644 --- a/src/Tizen.Multimedia.Util/Interop/Interop.ThumbnailExtractor.cs +++ b/src/Tizen.Multimedia.Util/Interop/Interop.ThumbnailExtractor.cs @@ -46,6 +46,13 @@ internal static partial class Interop [DllImport(Libraries.ThumbnailExtractor, EntryPoint = "thumbnail_util_destroy")] internal static extern ThumbnailExtractorError Destroy(IntPtr handle); + + [DllImport(Libraries.ThumbnailExtractor, EntryPoint = "thumbnail_util_extract_to_buffer")] + internal static extern ThumbnailExtractorError ExtractToBuffer(string path, uint width, uint height, out IntPtr thumbData, + out int dataSize, out uint thumbWidth, out uint thumbHeight); + + [DllImport(Libraries.ThumbnailExtractor, EntryPoint = "thumbnail_util_extract_to_file")] + internal static extern ThumbnailExtractorError ExtractToFile(string path, uint width, uint height, string thumbPath); } internal class ThumbnailExtractorHandle : CriticalHandle diff --git a/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractionResult.cs b/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractionResult.cs old mode 100755 new mode 100644 index 0e6b4b220..3149d7212 --- a/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractionResult.cs +++ b/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractionResult.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); @@ -14,6 +14,10 @@ * limitations under the License. */ +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + namespace Tizen.Multimedia.Util { /// @@ -22,9 +26,14 @@ namespace Tizen.Multimedia.Util /// 4 public class ThumbnailExtractionResult { - internal ThumbnailExtractionResult(byte[] thumbnailData, int width, int height) + internal ThumbnailExtractionResult(IntPtr nativeBuffer, int width, int height, int size) { - RawData = thumbnailData; + Debug.Assert(nativeBuffer != IntPtr.Zero); + + byte[] buf = new byte[size]; + Marshal.Copy(nativeBuffer, buf, 0, size); + + RawData = buf; Size = new Size(width, height); } diff --git a/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractor.cs b/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractor.cs index 45ccbe391..27e03431b 100644 --- a/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractor.cs +++ b/src/Tizen.Multimedia.Util/ThumbnailExtractor/ThumbnailExtractor.cs @@ -202,14 +202,11 @@ namespace Tizen.Multimedia.Util { try { - byte[] tmpBuf = new byte[dataSize]; - Marshal.Copy(thumbData, tmpBuf, 0, dataSize); - - tcs.TrySetResult(new ThumbnailExtractionResult(tmpBuf, thumbWidth, thumbHeight)); + tcs.TrySetResult(new ThumbnailExtractionResult(thumbData, thumbWidth, thumbHeight, dataSize)); } catch (Exception e) { - tcs.TrySetException(new InvalidOperationException("[" + error + "] Failed to copy data.", e)); + tcs.TrySetException(new InvalidOperationException("[" + error + "] Failed to create ThumbnailExtractionResult instance.", e)); } finally { @@ -242,5 +239,171 @@ namespace Tizen.Multimedia.Util tcs.TrySetCanceled(); }); } + + /// + /// Extracts the thumbnail for the given media with the specified path and size. + /// The generated thumbnail will be returned in . + /// + /// + /// The size of generated thumbnail will be 320x240.
+ /// If you want to set the size of generated thumbnail, please use
+ ///
+ /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage.
+ /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage. + ///
+ /// http://tizen.org/privilege/mediastorage + /// http://tizen.org/privilege/externalstorage + /// The path of the media file to extract the thumbnail. + /// is null. + /// does not exist. + /// An internal error occurs. + /// The specified file is not supported. + /// The caller has no required privilege. + /// The result of extracting operation. + /// 6 + public static ThumbnailExtractionResult Extract(string path) + { + return Extract(path, new Size(320, 240)); + } + + /// + /// Extracts the thumbnail for the given media with the specified path and size. + /// The generated thumbnail will be returned in . + /// + /// + /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage.
+ /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage. + ///
+ /// http://tizen.org/privilege/mediastorage + /// http://tizen.org/privilege/externalstorage + /// The path of the media file to extract the thumbnail. + /// The size of the thumbnail. + /// is null. + /// does not exist. + /// An internal error occurs. + /// + /// The width or the height of is less than or equal to zero. + /// + /// The specified file is not supported. + /// The caller has no required privilege. + /// The result of extracting operation. + /// 6 + public static ThumbnailExtractionResult Extract(string path, Size size) + { + if (path == null) + { + throw new ArgumentNullException(nameof(path)); + } + + if (File.Exists(path) == false) + { + throw new FileNotFoundException("File does not exists.", path); + } + + if (size.Width <= 0) + { + throw new ArgumentOutOfRangeException(nameof(size), size.Width, + "The width must be greater than zero."); + } + + if (size.Height <= 0) + { + throw new ArgumentOutOfRangeException(nameof(size), size.Height, + "The height must be greater than zero."); + } + + Native.ExtractToBuffer(path, (uint)size.Width, (uint)size.Height, out IntPtr thumbData, + out int dataSize, out uint thumbWidth, out uint thumbHeight). + ThrowIfError("Failed to extract thumbnail to buffer"); + + try + { + return new ThumbnailExtractionResult(thumbData, (int)thumbWidth, (int)thumbHeight, + dataSize); + } + finally + { + if (thumbData != IntPtr.Zero) + { + LibcSupport.Free(thumbData); + } + } + } + + /// + /// Extracts the thumbnail for the given media with the specified path and size. + /// The generated thumbnail will be saved in . + /// + /// + /// The size of generated thumbnail will be 320x240.
+ /// If you want to set the size of generated thumbnail, please use
+ ///
+ /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage.
+ /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage. + ///
+ /// http://tizen.org/privilege/mediastorage + /// http://tizen.org/privilege/externalstorage + /// The path of the media file to extract the thumbnail. + /// The path to save the generated thumbnail. + /// is null. + /// does not exist. + /// An internal error occurs. + /// The specified file is not supported. + /// The caller has no required privilege. + /// 6 + public static void Extract(string path, string resultThumbnailPath) + { + Extract(path, new Size(320, 240), resultThumbnailPath); + } + + /// + /// Extracts the thumbnail for the given media with the specified path and size. + /// The generated thumbnail will be saved in . + /// + /// + /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage.
+ /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage. + ///
+ /// http://tizen.org/privilege/mediastorage + /// http://tizen.org/privilege/externalstorage + /// The path of the media file to extract the thumbnail. + /// The size of the thumbnail. + /// The path to save the generated thumbnail. + /// is null. + /// does not exist. + /// An internal error occurs. + /// + /// The width or the height of is less than or equal to zero. + /// + /// The specified file is not supported. + /// The caller has no required privilege. + /// 6 + public static void Extract(string path, Size size, string resultThumbnailPath) + { + if (path == null) + { + throw new ArgumentNullException(nameof(path)); + } + + if (File.Exists(path) == false) + { + throw new FileNotFoundException("File does not exists.", path); + } + + if (size.Width <= 0) + { + throw new ArgumentOutOfRangeException(nameof(size), size.Width, + "The width must be greater than zero."); + } + + if (size.Height <= 0) + { + throw new ArgumentOutOfRangeException(nameof(size), size.Height, + "The height must be greater than zero."); + } + + Native.ExtractToFile(path, (uint)size.Width, (uint)size.Height, resultThumbnailPath). + ThrowIfError("Failed to extract thumbnail to file."); + } } }