fc2ブログ

ReonViewin'S HOBBY LABO

日頃の趣味活動について-最近物忘れが多くなったので忘れないための記録。一覧メニューは下の方にあります



Unityの記事   3Dプリンタの記事   CorelDRAW(HSX7)の記事   MovieStudio13の記事  
Reon Labo (YouTube)   家庭菜園の記事   その他の記事   リンク集

 

Uni補足00414 Unityで画像ファイルの書き出し


////////////////////////////////////////////

Unityで画像ファイルの書き出し

////////////////////////////////////////////
保存されたのを確認するためには

androidの場合は
/data/data/<アプリのID>/files/
なので、見つけてもいいけど

画面操作で、呼び出せるようにしてみた

ReadImgFile2Obj という名前の planeオブジェクトを作り

画面に並ぶように配置する。

--------- ReadImgFileScr.cs ----------------------

using UnityEngine;
using System.Collections;
using System.IO; //FileStream
#if UNITY_EDITOR
using UnityEditor;
#endif

public class ReadImgFileScr : MonoBehaviour
{
private string imgFile = "myphoto.png";
private string imgSaveFile = "mysave2img.png";

public Texture2D tex;
public Texture2D tex2;
private string path = "";

private string ErrorMessage = "";

void Start()
{
StartCoroutine(LoadImg());
}

byte[] LoadBin(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] buf = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
return buf;
}

IEnumerator LoadImg()
{
//string path = "";
GameObject readImgObj = GameObject.Find("ReadImgFileObj");

#if UNITY_EDITOR
path = Application.streamingAssetsPath + "\\" + imgFile;
Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(LoadBin(path));
readImgObj.GetComponent().material.mainTexture = tex;
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgFile;
WWW www = new WWW(path);
yield return www;
Texture2D tex = new Texture2D(0, 0);
readImgObj.GetComponent().material.mainTexture = www.texture;
#endif

}


IEnumerator LoadSaveImg()
{
//string path = "";
GameObject writeImgObj = GameObject.Find("ReadImgFile2Obj");
#if UNITY_EDITOR
path = Application.streamingAssetsPath + "\\" + imgSaveFile;

Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(LoadBin(path));
writeImgObj.GetComponent().material.mainTexture = tex;
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
//path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgSaveFile;
//path = Application.persistentDataPath + "/" + imgSaveFile;
//読み込む場所が違うので、読み込み方式が異なる
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] imageBytes = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(0, 0);
bool isloadbmpSuccess = tex.LoadImage(imageBytes);
writeImgObj.GetComponent().material.mainTexture = tex;
yield return new WaitForSeconds(0f);
#endif

}

IEnumerator SaveImg()
{
GameObject readImgObj = GameObject.Find("ReadImgFileObj");

#if UNITY_EDITOR
//画像の保存
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
// 使っているテクスチャをTexture2Dとして使う場合は as Texture2D を使う
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
string filePath = EditorUtility.SaveFilePanel("Save Texture", "", imgSaveFile, "png");
if (filePath.Length > 0)
{
// pngファイル保存.
File.WriteAllBytes(filePath, pngData);
}
//画像の保存
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
//path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgSaveFile;
path = Application.persistentDataPath + "/" + imgSaveFile;
//path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.

ErrorMessage = "FindFile";

ErrorMessage = "FindFile2";

if( pngData != null)
{
ErrorMessage = "FindFile3";

File.WriteAllBytes(path, pngData);

ErrorMessage = "書き込み";
}else{
ErrorMessage = "Error";
}

yield return new WaitForSeconds(0f);
#endif

}

void OnGUI()
{

if (GUI.Button(new Rect(300, 200, 100, 40), "画像保存"))
{
//StartCoroutine("LoadText2");
//StartCoroutine(SaveImg());//OK
//StartCoroutine(SaveImgNew());//ok
StartCoroutine(SaveImgOld());
}

if (GUI.Button(new Rect(300, 250, 100, 40), "画像確認"))
{
//StartCoroutine("LoadText2");
StartCoroutine(LoadSaveImg());
}

GUI.TextArea(new Rect(300, 300, 100, 40), ErrorMessage);
}


IEnumerator SaveImgOld()
{

GameObject readImgObj = GameObject.Find("ReadImgFileObj");

#if UNITY_EDITOR
//画像の保存
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
// 使っているテクスチャをTexture2Dとして使う場合は as Texture2D を使う
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
string filePath = EditorUtility.SaveFilePanel("Save Texture", "", imgSaveFile, "png");
if (filePath.Length > 0)
{
// pngファイル保存.
File.WriteAllBytes(filePath, pngData);
}
//画像の保存
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
//.WriteAllBytes方式
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
//path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgSaveFile;
//path = Application.persistentDataPath + "/" + imgSaveFile;
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.

ErrorMessage = "FindFile2";

if( pngData != null)
{
ErrorMessage = "FindFile3";

File.WriteAllBytes(path, pngData);

ErrorMessage = "書き込み";
}else{
ErrorMessage = "Error";
}

yield return new WaitForSeconds(0f);
#endif

}


IEnumerator SaveImgNew()
{
GameObject readImgObj = GameObject.Find("ReadImgFileObj");

#if UNITY_EDITOR
//画像の保存
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
// 使っているテクスチャをTexture2Dとして使う場合は as Texture2D を使う
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
string filePath = EditorUtility.SaveFilePanel("Save Texture", "", imgSaveFile, "png");
if (filePath.Length > 0)
{
// pngファイル保存.
File.WriteAllBytes(filePath, pngData);
}
//画像の保存
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
//.Write 方式
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
ErrorMessage = "1";
//tex2.ReadPixels (new Rect(0, 0, Screen.width, Screen.height), 0, 0);
using (FileStream BinaryFile = new FileStream(path, FileMode.Create, FileAccess.Write)) {
ErrorMessage = "2";
using (BinaryWriter Writer = new BinaryWriter(BinaryFile)) {
ErrorMessage = "3";
Writer.Write (tex2.EncodeToPNG());
ErrorMessage = "ED";
}
}

yield return new WaitForSeconds(0f);
#endif

}


}

--------- ReadImgFileScr.cs ----------------------


表示させる項目が増えてきたので
すべての void OnGUI(){ の下に
以下の部分を追記する
//解像度に依存せず
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(Screen.width / 800f, Screen.height / 600f, 1f));
関連記事

YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
にほんブログ村 IT技術ブログへにほんブログ村  FC2Blog Ranking

  1. 2017/03/29(水) 15:46:48|
  2. Unity
  3. | トラックバック:0
  4. | コメント:0
<<柿ピーが大好きなのですが・・・ | ホーム | 庭にピーマンの種を撒いた。2回目>>

コメント

コメントの投稿


管理者にだけ表示を許可する

トラックバック

トラックバック URL
http://moevoice.blog78.fc2.com/tb.php/233-bcb21803
この記事にトラックバックする(FC2ブログユーザー)
top
Unityでゲーム
uni01 シューティングゲーム
Uni37 迷路ゲームつくってみよう!001
Unityを使ってVR?(たぶん)

Uni補足0101 オブジェクトの非表示について考えてみる01
Uni補足0201 Prefab作成表示
Uni補足0301 パーティクルを表示させる
Uni補足0401 Unityで外部テキストファイルを読み書きしてみる。
Uni補足00412 Unityで外部画像ファイルの読み出し
Uni補足00414 Unityで画像ファイルの書き出し
Uni補足00415 指定したフォルダからの読み込み

top
3Dプリンタ
blenderで、3D文字を作る
文字と他のオブジェクトを合体させる
blenderで日本語文字を入力
blen004 引き出しボックス①
blen005 仕切り版の支えを作ってみた。まだ片方だけ・・・
blen006 スマホスタンドつくってみた。01
blen008 作ってみたら、思ったのとかけ離れた
blen009 コルクボード小物
top
Movie Studio 13
Movie Studio 13 Platinum アフレコ
Movie Studio 13 Platinum その1
Movie Studio 13 Platinum その2
Movie Studio 13 Platinum その3
Movie Studio 13 Platinum その4
Movie Studio 13 Platinum その5
Movie Studio その6 クロマキー合成
Movie Studio その7 音声変更(仮)
top
Font作り
いまさらだけどFONT作り
Inkscapeで初期設定
InkScape→FontForge→Font
top
イラスト
イラスト
LineStamp

広告1

諸事情により
投稿先を変更いたします。
REON LAB Kitchen Garden Photo

FC2ブログランキング

FC2Blog Ranking

プロフィール

Reon

Author:Reon
いつもゴロゴロしながら
リアル育成ゲームをしている
変わったいきもの
特徴:白い・昼行性
口癖:眠い・体が痛い
特殊能力:気象変動が数日前にわかる(居る場所のみ)

最近の記事

FC2拍手ランキング

アルバム

カテゴリー

最近のコメント

ブロとも一覧


Xジェンダー・Life

オジサンだって頑張れば出来るもん!!

毎日コツコツ植物の成長のように

笑丸や公式ブログ&おひさま大好き♪縁側日記

桂馬君の飛んでHappy

カナダから雑学発信

くろねの日記ブログ

京都美山高等学校 情報教員ブログ

ネットで稼ぐ。空いた時間で小遣いゲット♪

大阪からすぐの通信制 京都美山高校

岳の日記ブログ

楽しい家庭菜園

リンク

このブログをリンクに追加する

最近のトラックバック

Twitter

月別アーカイブ

ブロとも申請フォーム

この人とブロともになる

FC2カウンター

ランキング

にほんブログ村 IT技術ブログへ
にほんブログ村
にほんブログ村 IT技術ブログ Unityへ
にほんブログ村

ブログ内検索

RSSフィード

プラグインお試し