mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-19 14:20:22 +08:00
Image-source modules refactoring
This commit is contained in:

committed by
Vladimir Enchev

parent
2e782aecc5
commit
ae8b285fc0
37
image-source/image-source-common.ts
Normal file
37
image-source/image-source-common.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import http = require("http");
|
||||
|
||||
// This is used for definition purposes only, it does not generate JavaScript for it.
|
||||
import definition = require("image-source");
|
||||
|
||||
var RESOURCE_PREFIX = "res://";
|
||||
|
||||
export function fromResource(name: string): definition.ImageSource {
|
||||
var image = new definition.ImageSource();
|
||||
return image.loadFromResource(name) ? image : null;
|
||||
}
|
||||
|
||||
export function fromFile(path: string): definition.ImageSource {
|
||||
var image = new definition.ImageSource();
|
||||
return image.loadFromFile(path) ? image : null;
|
||||
}
|
||||
|
||||
export function fromData(data: any): definition.ImageSource {
|
||||
var image = new definition.ImageSource();
|
||||
return image.loadFromData(data) ? image : null;
|
||||
}
|
||||
|
||||
export function fromNativeSource(source: any): definition.ImageSource {
|
||||
var image = new definition.ImageSource();
|
||||
return image.setNativeSource(source) ? image : null;
|
||||
}
|
||||
|
||||
export function fromUrl(url: string): Promise<definition.ImageSource> {
|
||||
return http.getImage(url);
|
||||
}
|
||||
|
||||
export function fromFileOrResource(path: string): definition.ImageSource {
|
||||
if (path.indexOf(RESOURCE_PREFIX) === 0) {
|
||||
return fromResource(path.substr(RESOURCE_PREFIX.length));
|
||||
}
|
||||
return fromFile(path);
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
import appModule = require("application");
|
||||
|
||||
export function fromResource(name: string) {
|
||||
var androidApp = appModule.android;
|
||||
var res = androidApp.context.getResources();
|
||||
if (res) {
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||
if (0 < identifier) {
|
||||
// Load BitmapDrawable with getDrawable to make use of Android internal caching
|
||||
var bitmapDrawable = <android.graphics.drawable.BitmapDrawable>res.getDrawable(identifier);
|
||||
if (bitmapDrawable && bitmapDrawable.getBitmap) {
|
||||
return bitmapDrawable.getBitmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function fromFile(path: string) {
|
||||
return android.graphics.BitmapFactory.decodeFile(path, null);
|
||||
}
|
||||
|
||||
export function fromData(data: any) {
|
||||
return android.graphics.BitmapFactory.decodeStream(data);
|
||||
}
|
||||
|
||||
export function saveToFile(instance: android.graphics.Bitmap, path: string, format: number, quality = 100): boolean {
|
||||
if (!instance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var targetFormat = getTargetFromat(format);
|
||||
|
||||
// TODO add exception handling
|
||||
var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path));
|
||||
|
||||
var res = instance.compress(targetFormat, quality, outputStream);
|
||||
outputStream.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
export function toBase64String(instance: android.graphics.Bitmap, format: number, quality = 100): string {
|
||||
if (!instance) {
|
||||
return null;;
|
||||
}
|
||||
|
||||
var targetFormat = getTargetFromat(format);
|
||||
|
||||
var outputStream = new java.io.ByteArrayOutputStream();
|
||||
var base64Stream = new android.util.Base64OutputStream(outputStream, android.util.Base64.NO_WRAP);
|
||||
|
||||
instance.compress(targetFormat, quality, base64Stream);
|
||||
|
||||
base64Stream.close();
|
||||
outputStream.close();
|
||||
|
||||
return outputStream.toString();
|
||||
}
|
||||
|
||||
function getTargetFromat(format: number): android.graphics.Bitmap.CompressFormat {
|
||||
switch (format) {
|
||||
case 1: // JPEG
|
||||
return android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
default:
|
||||
return android.graphics.Bitmap.CompressFormat.PNG;
|
||||
}
|
||||
}
|
11
image-source/image-source-native.d.ts
vendored
11
image-source/image-source-native.d.ts
vendored
@ -1,11 +0,0 @@
|
||||
//@private
|
||||
/**
|
||||
* This module is used as a native implementation for each of the underlying platforms.
|
||||
* Users will not typically require it as it supports the module infrastructure.
|
||||
*/
|
||||
//
|
||||
export declare function fromResource(name: string): any;
|
||||
export declare function fromFile(path: string): any;
|
||||
export declare function fromData(data: any): any;
|
||||
export declare function saveToFile(instance: any, path: string, format: number, quality?: number): boolean;
|
||||
export declare function toBase64String(instance: any, format: number, quality?: number): string;
|
@ -1,55 +0,0 @@
|
||||
export var fromResource = function (name: string) {
|
||||
return UIImage.imageNamed(name);
|
||||
}
|
||||
|
||||
export var fromFile = function (path: string) {
|
||||
return UIImage.imageWithContentsOfFile(path);
|
||||
}
|
||||
|
||||
export var fromData = function (data: any) {
|
||||
return UIImage.imageWithData(data);
|
||||
}
|
||||
|
||||
export var saveToFile = function (instance: UIImage, path: string, format: number, quality?: number): boolean {
|
||||
var res = false;
|
||||
if (!instance) {
|
||||
return res;
|
||||
}
|
||||
|
||||
var data = getImageData(instance, format, quality);
|
||||
|
||||
if (data) {
|
||||
res = data.writeToFileAtomically(path, true);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function toBase64String(instance: UIImage, format: number, quality?: number): string {
|
||||
var res = null;
|
||||
if (!instance) {
|
||||
return res;
|
||||
}
|
||||
|
||||
var data = getImageData(instance, format, quality);
|
||||
|
||||
if (data) {
|
||||
res = data.base64Encoding();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function getImageData(instance: UIImage, format: number, quality: number): NSData {
|
||||
var data = null;
|
||||
switch (format) {
|
||||
case 0: // PNG
|
||||
data = UIImagePNGRepresentation(instance);
|
||||
break;
|
||||
case 1: // JPEG
|
||||
data = UIImageJPEGRepresentation(instance, ('undefined' === typeof quality) ? 1.0 : quality);
|
||||
break;
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
112
image-source/image-source.android.ts
Normal file
112
image-source/image-source.android.ts
Normal file
@ -0,0 +1,112 @@
|
||||
import types = require("utils/types");
|
||||
import fs = require("file-system");
|
||||
import appModule = require("application");
|
||||
import definition = require("image-source");
|
||||
import common = require("image-source/image-source-common");
|
||||
import enums = require("ui/enums");
|
||||
|
||||
// merge the exports of the common file with the exports of this file
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
export class ImageSource implements definition.ImageSource {
|
||||
public android: android.graphics.Bitmap;
|
||||
public ios: UIImage;
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
this.android = null;
|
||||
|
||||
var androidApp = appModule.android;
|
||||
var res = androidApp.context.getResources();
|
||||
if (res) {
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||
if (0 < identifier) {
|
||||
// Load BitmapDrawable with getDrawable to make use of Android internal caching
|
||||
var bitmapDrawable = <android.graphics.drawable.BitmapDrawable>res.getDrawable(identifier);
|
||||
if (bitmapDrawable && bitmapDrawable.getBitmap) {
|
||||
this.android = bitmapDrawable.getBitmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
var fileName = types.isString(path) ? path.trim() : "";
|
||||
if (fileName.indexOf("~/") === 0) {
|
||||
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
|
||||
}
|
||||
|
||||
this.android = android.graphics.BitmapFactory.decodeFile(fileName, null);
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.android = android.graphics.BitmapFactory.decodeStream(data);
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
this.android = source;
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: string, quality = 100): boolean {
|
||||
if (!this.android) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var targetFormat = getTargetFromat(format);
|
||||
|
||||
// TODO add exception handling
|
||||
var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path));
|
||||
|
||||
var res = this.android.compress(targetFormat, quality, outputStream);
|
||||
outputStream.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
public toBase64String(format: string, quality = 100): string {
|
||||
if (!this.android) {
|
||||
return null;;
|
||||
}
|
||||
|
||||
var targetFormat = getTargetFromat(format);
|
||||
|
||||
var outputStream = new java.io.ByteArrayOutputStream();
|
||||
var base64Stream = new android.util.Base64OutputStream(outputStream, android.util.Base64.NO_WRAP);
|
||||
|
||||
this.android.compress(targetFormat, quality, base64Stream);
|
||||
|
||||
base64Stream.close();
|
||||
outputStream.close();
|
||||
|
||||
return outputStream.toString();
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
if (this.android) {
|
||||
return this.android.getHeight();
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
if (this.android) {
|
||||
return this.android.getWidth();
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
|
||||
function getTargetFromat(format: string): android.graphics.Bitmap.CompressFormat {
|
||||
switch (format) {
|
||||
case enums.ImageFormat.jpeg:
|
||||
return android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
default:
|
||||
return android.graphics.Bitmap.CompressFormat.PNG;
|
||||
}
|
||||
}
|
27
image-source/image-source.d.ts
vendored
27
image-source/image-source.d.ts
vendored
@ -3,22 +3,7 @@
|
||||
*/
|
||||
declare module "image-source" {
|
||||
|
||||
/**
|
||||
* Defines the recognized image formats.
|
||||
*/
|
||||
export enum ImageFormat {
|
||||
/**
|
||||
* The W3C Portable Network Graphics (PNG) image format.
|
||||
*/
|
||||
PNG,
|
||||
|
||||
/**
|
||||
* The Joint Photographic Experts Group (JPEG) image format.
|
||||
*/
|
||||
JPEG,
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Encapsulates the common abstraction behind a platform specific object (typically a Bitmap) that is used as a source for images.
|
||||
*/
|
||||
export class ImageSource {
|
||||
@ -73,14 +58,14 @@ declare module "image-source" {
|
||||
* @param format The format (encoding) of the image.
|
||||
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality.
|
||||
*/
|
||||
saveToFile(path: string, format: ImageFormat, quality?: number): boolean;
|
||||
saveToFile(path: string, format: string, quality?: number): boolean;
|
||||
|
||||
/**
|
||||
* Converts the image to base64 encoded string, using the provided image format and quality.
|
||||
* @param format The format (encoding) of the image.
|
||||
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality.
|
||||
*/
|
||||
toBase64String(format: ImageFormat, quality?: number): string;
|
||||
toBase64String(format: string, quality?: number): string;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,4 +98,10 @@ declare module "image-source" {
|
||||
* @param url The link to the remote image object. This operation will download and decode the image.
|
||||
*/
|
||||
export function fromUrl(url: string): Promise<ImageSource>;
|
||||
|
||||
/**
|
||||
* Creates a new ImageSource instance and loads it from the specified local file or resource(if spexified with "res://" prefix)
|
||||
* @param path The location of the file on the file system.
|
||||
*/
|
||||
export function fromFileOrResource(path: string): ImageSource;
|
||||
}
|
100
image-source/image-source.ios.ts
Normal file
100
image-source/image-source.ios.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import definition = require("image-source");
|
||||
import types = require("utils/types");
|
||||
import fs = require("file-system");
|
||||
import common = require("image-source/image-source-common");
|
||||
import enums = require("ui/enums");
|
||||
|
||||
// merge the exports of the common file with the exports of this file
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
export class ImageSource implements definition.ImageSource {
|
||||
public android: android.graphics.Bitmap;
|
||||
public ios: UIImage;
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
this.ios = UIImage.imageNamed(name);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
var fileName = types.isString(path) ? path.trim() : "";
|
||||
|
||||
if (fileName.indexOf("~/") === 0) {
|
||||
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
|
||||
}
|
||||
|
||||
this.ios = UIImage.imageWithContentsOfFile(fileName);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.ios = UIImage.imageWithData(data);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
this.ios = source;
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: string, quality?: number): boolean {
|
||||
if (!this.ios) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var data = getImageData(this.ios, format, quality);
|
||||
|
||||
if (data) {
|
||||
return data.writeToFileAtomically(path, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public toBase64String(format: string, quality?: number): string {
|
||||
var res = null;
|
||||
if (!this.ios) {
|
||||
return res;
|
||||
}
|
||||
|
||||
var data = getImageData(this.ios, format, quality);
|
||||
|
||||
if (data) {
|
||||
res = data.base64Encoding();
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
if (this.ios) {
|
||||
return this.ios.size.height;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
if (this.ios) {
|
||||
return this.ios.size.width;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
|
||||
function getImageData(instance: UIImage, format: string, quality = 1.0): NSData {
|
||||
var data = null;
|
||||
switch (format) {
|
||||
case enums.ImageFormat.png: // PNG
|
||||
data = UIImagePNGRepresentation(instance);
|
||||
break;
|
||||
case enums.ImageFormat.jpeg: // JPEG
|
||||
data = UIImageJPEGRepresentation(instance, quality);
|
||||
break;
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
import native = require("image-source/image-source-native");
|
||||
import platform = require("platform");
|
||||
import types = require("utils/types");
|
||||
import fs = require("file-system");
|
||||
import http = require("http");
|
||||
|
||||
// This is used for definition purposes only, it does not generate JavaScript for it.
|
||||
import definition = require("image-source");
|
||||
|
||||
export enum ImageFormat {
|
||||
PNG,
|
||||
JPEG,
|
||||
}
|
||||
|
||||
// TODO: Refactor into two files (.android.ts & .ios.ts);
|
||||
export class ImageSource {
|
||||
public android: android.graphics.Bitmap;
|
||||
public ios: UIImage;
|
||||
|
||||
constructor() {
|
||||
this.setNativeInstance(null);
|
||||
}
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
var nativeInstance = native.fromResource(name);
|
||||
this.setNativeInstance(nativeInstance);
|
||||
return nativeInstance != null;
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
var fileName = types.isString(path) ? path.trim() : "";
|
||||
|
||||
if (fileName.indexOf("~/") === 0) {
|
||||
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
|
||||
}
|
||||
|
||||
var nativeInstance = native.fromFile(fileName);
|
||||
this.setNativeInstance(nativeInstance);
|
||||
return (nativeInstance != null);
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
var nativeInstance = native.fromData(data);
|
||||
this.setNativeInstance(nativeInstance);
|
||||
return (nativeInstance != null);
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
this.setNativeInstance(source);
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: ImageFormat, quality?: number): boolean {
|
||||
return native.saveToFile(this.getNativeInstance(), path, format, quality);
|
||||
}
|
||||
|
||||
public toBase64String(format: ImageFormat, quality?: number): string {
|
||||
return native.toBase64String(this.getNativeInstance(), format, quality);
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
// TODO: Refactor this, use class inheritance to overcome these switches
|
||||
if (this.android) {
|
||||
return this.android.getHeight();
|
||||
}
|
||||
if (this.ios) {
|
||||
return this.ios.size.height;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
// TODO: Refactor this, use class inheritance to overcome these switches
|
||||
if (this.android) {
|
||||
return this.android.getWidth();
|
||||
}
|
||||
if (this.ios) {
|
||||
return this.ios.size.width;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
private setNativeInstance(instance: any) {
|
||||
// TODO: Refactor this, use class inheritance to overcome these switches
|
||||
if (platform.device.os === platform.platformNames.android) {
|
||||
this.android = instance;
|
||||
} else if (platform.device.os === platform.platformNames.ios) {
|
||||
this.ios = instance;
|
||||
}
|
||||
}
|
||||
|
||||
private getNativeInstance(): any {
|
||||
// TODO: Refactor this, use class inheritance to overcome these switches
|
||||
if (this.android) {
|
||||
return this.android;
|
||||
}
|
||||
if (this.ios) {
|
||||
return this.ios;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function fromResource(name: string): ImageSource {
|
||||
var image = new ImageSource();
|
||||
return image.loadFromResource(name) ? image : null;
|
||||
}
|
||||
|
||||
export function fromFile(path: string): ImageSource {
|
||||
var image = new ImageSource();
|
||||
return image.loadFromFile(path) ? image : null;
|
||||
}
|
||||
|
||||
export function fromData(data: any): ImageSource {
|
||||
var image = new ImageSource();
|
||||
return image.loadFromData(data) ? image : null;
|
||||
}
|
||||
|
||||
export function fromNativeSource(source: any): ImageSource {
|
||||
var image = new ImageSource();
|
||||
return image.setNativeSource(source) ? image : null;
|
||||
}
|
||||
|
||||
export function fromUrl(url: string): Promise<definition.ImageSource> {
|
||||
return http.getImage(url);
|
||||
}
|
Reference in New Issue
Block a user