Exemplo n.º 1
0
        /// <summary>
        /// Extracts the thumbnail for the given media with the specified path and size.
        /// The generated thumbnail will be saved in <paramref name="resultThumbnailPath"/>.
        /// </summary>
        /// <remarks>
        /// The size of <paramref name="resultThumbnailPath"/> image will be <paramref name="size"/>.<br/>
        /// But, if the size of <paramref name="path"/> has different ratio with <paramref name="size"/>,<br/>
        /// thumbnail will be generated in a way to keep the ratio of <paramref name="path"/>, which based on short axis of <paramref name="path"/>.<br/>
        /// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1) and <paramref name="size"/> is 320x240,<br/>
        /// the size of <paramref name="resultThumbnailPath"/> is 432x240(1.8:1).<br/>
        /// <br/>
        /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
        /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
        /// </remarks>
        /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
        /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
        /// <param name="path">The path of the media file to extract the thumbnail.</param>
        /// <param name="size">The size of the thumbnail.</param>
        /// <param name="resultThumbnailPath">The path to save the generated thumbnail.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
        /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     The width or the height of <paramref name="size"/> is less than or equal to zero.
        /// </exception>
        /// <exception cref="FileFormatException">The specified file is not supported.</exception>
        /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
        /// <since_tizen> 6 </since_tizen>
        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.");
        }
Exemplo n.º 2
0
        private static async Task <ThumbnailExtractionResult> ExtractAsyncCore(string path, Size?size,
                                                                               CancellationToken cancellationToken)
        {
            using (var handle = CreateHandle())
            {
                Native.SetPath(handle, path).ThrowIfError("Failed to extract; failed to set the path.");

                if (size.HasValue)
                {
                    Native.SetSize(handle, size.Value.Width, size.Value.Height).
                    ThrowIfError("Failed to extract; failed to set the size");
                }

                var tcs = new TaskCompletionSource <ThumbnailExtractionResult>();

                IntPtr id = IntPtr.Zero;

                try
                {
                    Native.Extract(handle, GetCallback(tcs), IntPtr.Zero, out id)
                    .ThrowIfError("Failed to extract.");

                    using (RegisterCancellationToken(tcs, cancellationToken, handle, Marshal.PtrToStringAnsi(id)))
                    {
                        return(await tcs.Task);
                    }
                }
                finally
                {
                    LibcSupport.Free(id);
                }
            }
        }
Exemplo n.º 3
0
        private static IDisposable RegisterCancellationToken(TaskCompletionSource <ThumbnailExtractionResult> tcs,
                                                             CancellationToken cancellationToken, Handle handle, string id)
        {
            if (cancellationToken.CanBeCanceled == false)
            {
                return(null);
            }

            return(cancellationToken.Register(() =>
            {
                if (tcs.Task.IsCompleted)
                {
                    return;
                }

                Native.Cancel(handle, id).ThrowIfError("Failed to cancel.");
                tcs.TrySetCanceled();
            }));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extracts the thumbnail for the given media with the specified path and size.
        /// The generated thumbnail will be returned in <see cref="ThumbnailExtractionResult"/>.
        /// </summary>
        /// <remarks>
        /// The size of generated thumbnail will be <paramref name="size"/>.<br/>
        /// But, if the size of <paramref name="path"/> has different ratio with <paramref name="size"/>,<br/>
        /// thumbnail will be generated in a way to keep the ratio of <paramref name="path"/>, which based on short axis of <paramref name="path"/>.<br/>
        /// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1)) and <paramref name="size"/> is 320x240,<br/>
        /// the size of generated thumbnail is 432x240(1.8:1).<br/>
        /// <br/>
        /// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
        /// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
        /// </remarks>
        /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
        /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
        /// <param name="path">The path of the media file to extract the thumbnail.</param>
        /// <param name="size">The size of the thumbnail.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
        /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     The width or the height of <paramref name="size"/> is less than or equal to zero.
        /// </exception>
        /// <exception cref="FileFormatException">The specified file is not supported.</exception>
        /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
        /// <returns>The result of extracting operation.</returns>
        /// <since_tizen> 6 </since_tizen>
        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);
                }
            }
        }