Skip to content

Instantly share code, notes, and snippets.

@yagitoshiro
Created August 12, 2012 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagitoshiro/3333058 to your computer and use it in GitHub Desktop.
Save yagitoshiro/3333058 to your computer and use it in GitHub Desktop.
画像ファイルを読ませると幅と高さを持ったTiBlobを返す
/**
* This file was auto-generated by the Titanium Module SDK helper for Android
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*
* var mod = require('org.selfkleptomaniac.ti.mod.stupidimagereader');
* var image = "washingmachine.jpg";
*
* var blob = mod.readImage("Resources/" + image); //Resources以下のファイルはこうやって読み込んでね
* Ti.API.info("++++++++++++++++++++start+++++++++++++++++++");
* Ti.API.info(image);
* Ti.API.info(JSON.stringify(blob));
* if(blob){
* Ti.API.info(blob.width);
* Ti.API.info(blob.height);
* }
* Ti.API.info("++++++++++++++++++++end+++++++++++++++++++");
*
*
* var old_file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + image);
* var new_file = Ti.Filesystem.tempDirectory+ "test.jpg";
* old_file.copy(new_file);
*
* var new_data = Ti.Filesystem.getFile(new_file);
* if(new_data.exists()){
* var blob2 = mod.readImage(new_file);
* Ti.API.info("++++++++++++++++++++start+++++++++++++++++++");
* Ti.API.info(new_file);
* Ti.API.info(JSON.stringify(blob2));
* if(blob2){
* Ti.API.info(blob2.width);
* Ti.API.info(blob2.height);
* }
* Ti.API.info("++++++++++++++++++++end+++++++++++++++++++");
* }else{
* Ti.API.info("++++++++++++++++++++error+++++++++++++++++++");
* Ti.API.info("FILE NOT COPIED");
* Ti.API.info("++++++++++++++++++++error+++++++++++++++++++");
* }
*/
package org.selfkleptomaniac.ti.mod.stupidimagereader;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.TiBlob;
import android.app.Activity;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.util.regex.*;
import java.nio.channels.FileChannel;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.res.AssetManager;
@Kroll.module(name="Stupidimagereader", id="org.selfkleptomaniac.ti.mod.stupidimagereader")
public class StupidimagereaderModule extends KrollModule
{
// Standard Debugging variables
private static final String LCAT = "StupidimagereaderModule";
private static final boolean DBG = TiConfig.LOGD;
// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;
public StupidimagereaderModule()
{
super();
}
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(LCAT, "inside onAppCreate");
// put module init code that needs to run when the application is created
}
// Methods
@Kroll.method
public String example()
{
Log.d(LCAT, "example called");
return "hello world";
}
// Properties
@Kroll.getProperty
public String getExampleProp()
{
Log.d(LCAT, "get example property");
return "hello world";
}
@Kroll.setProperty
public void setExampleProp(String value) {
Log.d(LCAT, "set example property: " + value);
}
@Kroll.method
public TiBlob readImage(String path){
String fpath = null;
String save_path = null;
InputStream is = null;
if(!path.startsWith("Resources")){
Pattern pattern = Pattern.compile("((file|content|appdata-private|app)://)");
Matcher matcher = pattern.matcher(path);
path = matcher.replaceFirst("");
if (!path.startsWith("/")) {
path = "/" + path;
}
}
fpath = path;
File save_path_base = new File(path);
save_path = "stupidimagereader/" + save_path_base.getName();
String toFile = "/data/data/"+ TiApplication.getInstance().getPackageName() +"/app_appdata/" + save_path;
try{
copyFile(path, toFile);
TiBlob image = null;
try{
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
opts.inJustDecodeBounds = false;
Bitmap image_base = BitmapFactory.decodeFile(toFile, opts);
image = returnBlob(image_base);
}catch(NullPointerException e){
Log.d(LCAT, "Nullpo");
}finally{
File cleanup = new File(toFile);
cleanup.delete();
}
return image;
}catch(IOException e){
Log.d(LCAT, "IOPO");
Log.d(LCAT, fpath);
Log.d(LCAT, toFile);
return null;
}
}
private TiBlob returnBlob(Bitmap image_base) throws NullPointerException {
TiBlob blob = TiBlob.blobFromImage(image_base);
return blob;
}
private void copyFile(String src, String dstFilePath) throws IOException{
File dstFile = new File(dstFilePath);
String parent_dir = dstFile.getParent();
File dir = new File(parent_dir);
dir.mkdirs();
if(src.startsWith("Resources/")){
Activity activity = getActivity();
AssetManager as = activity.getResources().getAssets();
InputStream input = as.open(src);
OutputStream output = null;
output = new FileOutputStream(dstFile);
int DEFAULT_BUFFER_SIZE = 1024 * 4;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
input.close();
output.close();
}else{
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dstFilePath).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null){
inChannel.close();
}
if (outChannel != null){
outChannel.close();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment