forked from almas/cordova-plugin-thumbnail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Davaadorj
committed
Apr 20, 2018
1 parent
d58cb31
commit cda5b21
Showing
10 changed files
with
632 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# cordova-plugin-thumbnail | ||
|
||
This plug-in implements the ability to generate image thumbnails for Cordova project. Support Android and iOS. | ||
|
||
# Installation | ||
|
||
``` | ||
cordova plugin add https://github.com/almas/cordova-plugin-thumbnail.git | ||
``` | ||
|
||
# How to use | ||
|
||
`Thumbnails.thumbnail(srcPath, width, height, [options,] successFn, failFn)` | ||
|
||
explain: | ||
|
||
- `srcPath`: Image path. Supported path format: `file:///path/to/spot`. | ||
- `width`: Width of thumbnail | ||
- `height` Height of thumbnail | ||
- `options` other configuration parameter objects | ||
- `toPath` Thumbnail storage path, if not specified, will randomly create a file to store the generated thumbnail. | ||
- `successFn` Thumbnail generates a successful callback function | ||
- `failFn` Thumbnail Generation Failed Callback Function | ||
|
||
### Example: ### | ||
|
||
``` | ||
Thumbnails.thumbnail(srcPath, width, height, function success(path) { | ||
console.log ("thumbnails generated in:" + path); | ||
}, function fail(error) { | ||
console.error(error); | ||
}); | ||
``` | ||
|
||
## Android specific configuration: | ||
|
||
You need to add the following configuration in the `config.xml` file: | ||
|
||
``` | ||
<preference name="AndroidPersistentFileLocation" value="Internal" /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" | ||
id="com.cordova.plugin.thumbnail" version="0.1.0"> | ||
<name>Thumbnail</name> | ||
<description>Cordova Thumbnail Plugin</description> | ||
<license>Apache 2.0</license> | ||
<keywords>cordova,thumbnail,Android,iOS</keywords> | ||
|
||
<js-module src="www/thumbnail.js" name="Thumbnails"> | ||
<clobbers target="window.Thumbnails" /> | ||
</js-module> | ||
|
||
<platform name="android"> | ||
<config-file target="res/xml/config.xml" parent="/*"> | ||
<feature name="Thumbnails"> | ||
<param name="android-package" value="com.cordova.plugin.thumbnail.ThumbnailsCordovaPlugin"/> | ||
<param name="onload" value="true" /> | ||
</feature> | ||
</config-file> | ||
|
||
<!-- <config-file target="AndroidManifest.xml" parent="/*"> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
</config-file> --> | ||
|
||
<source-file src="src/android/SourcePathNotFoundException.java" target-dir="src/com/cordova/plugin/thumbnail" /> | ||
<source-file src="src/android/TargetPathNotFoundException.java" target-dir="src/com/cordova/plugin/thumbnail" /> | ||
<source-file src="src/android/Thumbnails.java" target-dir="src/com/cordova/plugin/thumbnail" /> | ||
<source-file src="src/android/ThumbnailsCordovaPlugin.java" target-dir="src/com/cordova/plugin/thumbnail" /> | ||
</platform> | ||
|
||
<platform name="ios"> | ||
<config-file target="config.xml" parent="/*"> | ||
<feature name="Thumbnails"> | ||
<param name="ios-package" value="ThumbnailCordovaPlugin"/> | ||
<param name="onload" value="true" /> | ||
</feature> | ||
</config-file> | ||
|
||
<header-file src="src/ios/Thumbnail.h" /> | ||
<source-file src="src/ios/Thumbnail.m" /> | ||
|
||
<framework src="MobileCoreServices.framework" /> | ||
<framework src="CoreGraphics.framework" /> | ||
<framework src="CoreImage.framework" /> | ||
<framework src="ImageIO.framework" /> | ||
</platform> | ||
</plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.cordova.plugin.thumbnail; | ||
|
||
public class SourcePathNotFoundException extends RuntimeException { | ||
|
||
public SourcePathNotFoundException() { | ||
} | ||
|
||
public SourcePathNotFoundException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.cordova.plugin.thumbnail; | ||
|
||
public class TargetPathNotFoundException extends RuntimeException { | ||
|
||
public TargetPathNotFoundException() { | ||
} | ||
|
||
public TargetPathNotFoundException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.cordova.plugin.thumbnail; | ||
|
||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.media.ThumbnailUtils; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class Thumbnails { | ||
|
||
public static void thumbnail(Options thumbnailOptions) throws IOException { | ||
long begin = System.currentTimeMillis(); | ||
BitmapFactory.Options options = calculateImageSize(thumbnailOptions.sourcePath); | ||
|
||
options.inJustDecodeBounds = false; | ||
options.inSampleSize = calculateInSampleSize(options, thumbnailOptions.width, thumbnailOptions.height); | ||
boolean needScaleImage = options.outWidth / options.inSampleSize > thumbnailOptions.width && | ||
options.outHeight / options.inSampleSize > thumbnailOptions.height; | ||
|
||
Bitmap bitmap = BitmapFactory.decodeFile(thumbnailOptions.sourcePath, options); | ||
|
||
if (needScaleImage) { | ||
bitmap = thumbnailSamllImage(bitmap, thumbnailOptions.width, thumbnailOptions.height); | ||
} | ||
|
||
saveBitmapToFile(bitmap, thumbnailOptions.targetPath); | ||
|
||
Log.i("thumbnail", "产生[" + thumbnailOptions.sourcePath + "]缩略图共花费了" + (System.currentTimeMillis() - begin) + "ms"); | ||
} | ||
|
||
private static Bitmap thumbnailSamllImage(Bitmap bitmap, int width, int height) { | ||
long begin = System.currentTimeMillis(); | ||
int oWidth = bitmap.getWidth(); | ||
int oHeight = bitmap.getHeight(); | ||
|
||
float ratio = Math.min(oWidth * 1.0f / width, oHeight * 1.0f / height); | ||
|
||
width = (int)(oWidth / ratio); | ||
height = (int)(oHeight / ratio); | ||
|
||
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height); | ||
|
||
Log.i("thumbnailSallImage", "花费:" + (System.currentTimeMillis() - begin) + "ms"); | ||
return bitmap; | ||
} | ||
|
||
public static BitmapFactory.Options calculateImageSize(String sourcePath) throws IOException { | ||
InputStream is = null; | ||
try { | ||
is = new BufferedInputStream(new FileInputStream(sourcePath)); | ||
BitmapFactory.Options options = new BitmapFactory.Options(); | ||
options.inJustDecodeBounds = true; | ||
BitmapFactory.decodeStream(is, null, options); | ||
return options; | ||
} catch (FileNotFoundException e) { | ||
throw new SourcePathNotFoundException(e); | ||
}finally { | ||
if (is != null) { | ||
is.close(); | ||
} | ||
} | ||
} | ||
|
||
public static int calculateInSampleSize(BitmapFactory.Options options, | ||
int reqWidth, int reqHeight) { | ||
final int height = options.outHeight; | ||
final int width = options.outWidth; | ||
int inSampleSize = 1; | ||
|
||
options.inSampleSize = 8; | ||
|
||
if (height > reqHeight || width > reqWidth) { | ||
final int halfHeight = height / 2; | ||
final int halfWidth = width / 2; | ||
|
||
while ((halfHeight / inSampleSize) > reqHeight && | ||
(halfWidth / inSampleSize) > reqWidth) { | ||
inSampleSize *= 2; | ||
} | ||
} | ||
|
||
return inSampleSize; | ||
} | ||
|
||
public static void saveBitmapToFile(Bitmap bitmap, String targetPath) { | ||
OutputStream os = null; | ||
|
||
try { | ||
os = new BufferedOutputStream(new FileOutputStream(targetPath)); | ||
bitmap.compress(guessImageType(targetPath), 90, os); | ||
} catch (FileNotFoundException ex) { | ||
throw new TargetPathNotFoundException(ex); | ||
} catch (IOException ex) { | ||
Log.e("Thumbnails.saveBitmapToFile()", "打开文件流出错:" + targetPath); | ||
ex.printStackTrace(); | ||
} finally { | ||
if (os != null) { | ||
try { | ||
os.close(); | ||
} catch (IOException ex) { | ||
Log.e("Thumbnails.saveBitmapToFile()", "关闭文件流出错。"); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static Bitmap.CompressFormat guessImageType(String filePath) { | ||
String fileExt = filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase(); | ||
if (fileExt.equals("png")) { | ||
return Bitmap.CompressFormat.PNG; | ||
} else if (fileExt.equals("webp")) { | ||
return Bitmap.CompressFormat.WEBP; | ||
} else { | ||
return Bitmap.CompressFormat.JPEG; | ||
} | ||
} | ||
|
||
public static class Options { | ||
public String targetPath; | ||
public String sourcePath; | ||
public int width; | ||
public int height; | ||
|
||
public Options() { | ||
} | ||
} | ||
} |
Oops, something went wrong.