16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ricoh thetaのJPGから角度を取り出すコード

Last updated at Posted at 2013-11-20

RICOH SHETAから角度を取り出すコード@C#版
MIROさんから頂いたコードを元に改造しました。
UNITYで使用することを前提にしています。

ViewPict.cs
using UnityEngine;
using System.Collections;
using System.IO;

public class ViewPict : MonoBehaviour 
{
	static int SerchAngle(string fileName)
	{
		BinaryReader br = new BinaryReader(new FileStream("Assets/Resources/" + fileName+".jpg", FileMode.Open));
		byte [] buf = br.ReadBytes((int)br.BaseStream.Length );
		br.Close();
		br = null;
		
		int ix;
		int offset;
		float compass;
		float zenith_x;
		float zenith_y;



		// check EXIF
		// find CompassEs
		ix = find_data(buf,new byte[] {0x00,0x04,0x00,0x05,0x00,0x00,0x00,0x01});
		if (ix<0) 
		{
			return -1; 
		}
		offset = get_dword(buf,ix)+12;
		compass = parse_u_rational(buf,offset);

		// find ZenithEs
		ix = find_data(buf,new byte[] {0x00,0x03,0x00,0x0A,0x00,0x00,0x00,0x02});
		if (ix<0) 
		{
			return -1;
		}
		offset = get_dword(buf,ix)+12;
		zenith_x = parse_u_rational(buf,offset  );
		zenith_y = parse_u_rational(buf,offset+8);

		Debug.Log( "1:" + zenith_y.ToString() + ",2:" + zenith_x.ToString());
		return 0; 
		//sphere.transform.Rotate(0,0,-zenith_y);
		//sphere.transform.Rotate(zenith_x,0,0);
	}

	static int find_data(byte[] buf, byte[] pattern)
	{
		int max;

		max = buf.Length;
		
		if (max>64*1000) 
		{ 
			max = 64*1000; 
		}


		for (int i = 0; i < max; i++)
		{
			int iTemp = i;
			for (int p = 0; p < pattern.Length ; p++, iTemp++)
			{
				if (pattern[p] != buf[iTemp] || iTemp >= max)
				{
					break;
				}

				if (p == pattern.Length - 1)
				{
					return iTemp + 1;
				}
			}
		}

		return -1;
	}

	static int get_dword(byte[] buf, int offset)
	{

		return (buf[offset]<<24) | (buf[offset+1]<<16) | (buf[offset+2]<<8) | buf[offset+3];
	}

	static float parse_u_rational(byte[] buf, int offset) 
	{
		float a;
		float b;
		a = get_dword(buf,offset  );
		b = get_dword(buf,offset+4);
		return a/b;
	}
}
16
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?