Camera/Photo code
by Ecliptic · in iTorque 2D · 02/21/2009 (2:33 pm) · 36 replies
Hey guys! I have been digging around trying to see how I can access the photo library on the iPhone and I found the code how to access it as well as utilizing the camera. I thought I would post here to share with the rest of you if you needed it for later use. Now this is not Torque script, it is the Object-C code that is used on the iphone. I am not sure how you would go about and add this to torque but in the even you need it here you go.
//Make sure the Library isn't empty
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
[currentViewcontroller presentModalViewController:picker animated:YES];
}
//To use the Camera use similar code. Just replace source type with Camera.
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsImageEditing = YES;
picker.delegate = self;
[currentViewcontroller presentModalViewController:picker animated:YES];
}
//The assigned Delegate must use this method to recieve the controller.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
//work with the finished image as needed.
[self saveImage:image];
//or you maye Get the original image
UIImage *originalImage = [editingInfo valueForKey:UIImagePickerControllerOriginalImage];
//Save the edit crop as metadata
[self saveTagZone:[editingInfo valueForKey:UIImagePickerControllerCropRect]];
}
#2
I have found some info under the IPhone project in the ITGB folder. Under PlaterofrmiPhone they have the TGBAppDelegate.mm and the iPhoneDialogs.mm. I just don't know what torque is using to reference them or if they are even utilized.
Hopefully we can get something working. I seen some apps where you can take pictures from the actual apps. Would be nice if we could get something like that working with torque.
Dane
02/21/2009 (5:41 pm)
That would be awesome! I am trying to work on it now and I will let you know if I come up with anything but it is probably out of my skill level. I am going to see if I can find where the OpenFileDialog section is in the torque code and see if I can modify it to use this.I have found some info under the IPhone project in the ITGB folder. Under PlaterofrmiPhone they have the TGBAppDelegate.mm and the iPhoneDialogs.mm. I just don't know what torque is using to reference them or if they are even utilized.
Hopefully we can get something working. I seen some apps where you can take pictures from the actual apps. Would be nice if we could get something like that working with torque.
Dane
#3
Here's my understanding right now:
1. TGB does not capture input in script and send event to platformiPhone. Just the opposite: the iPhone platform code captures a touch, creates a mouse event out of it, and sends the event to TGB.
2. Taking a screen shot on an iPod Touch requires you to press the power and home button for 1-2 seconds, then releasing. This will take a picture. I do not have an iPhone, but I'm assuming there is a button that will take a picture with the camera.
3. You can view pictures by going to the Photo Library.
4. Taking a picture is an input (receive message from iPhone), whereas displaying a picture is a rendering/function call (send message to iPhone).
With the above logic (which could be flawed), I have two options for taking a picture:
1. Take picture normally (press button(s))
2. Create a GUIButtonCtrl in TGB that will take a picture by calling a TorqueScript function
For displaying pictures:
1. Display the sub-view window of a UIImagePickerControllerSourceTypePhotoLibrary picker.
2. Grab image data from a Photo Library file, and render that onto a GUI control in TGB
@Ecliptic - I'm going to attempt to take a picture from TorqueScript, similar to this:
Simple pic
But hopefully I can get to this:
Advanced pic
I won't think about rendering the actual image just yet. I think if I can get image capturing down, I can tackle rendering unless someone else will take that.
How does that plan of action sound?
02/22/2009 (10:28 am)
Alright, so I'm digging through code now. Up until this point, I've mostly focused on the TGB interface of things. I haven't really dived into the Objective-C, platformiPhone code.Here's my understanding right now:
1. TGB does not capture input in script and send event to platformiPhone. Just the opposite: the iPhone platform code captures a touch, creates a mouse event out of it, and sends the event to TGB.
2. Taking a screen shot on an iPod Touch requires you to press the power and home button for 1-2 seconds, then releasing. This will take a picture. I do not have an iPhone, but I'm assuming there is a button that will take a picture with the camera.
3. You can view pictures by going to the Photo Library.
4. Taking a picture is an input (receive message from iPhone), whereas displaying a picture is a rendering/function call (send message to iPhone).
With the above logic (which could be flawed), I have two options for taking a picture:
1. Take picture normally (press button(s))
2. Create a GUIButtonCtrl in TGB that will take a picture by calling a TorqueScript function
For displaying pictures:
1. Display the sub-view window of a UIImagePickerControllerSourceTypePhotoLibrary picker.
2. Grab image data from a Photo Library file, and render that onto a GUI control in TGB
@Ecliptic - I'm going to attempt to take a picture from TorqueScript, similar to this:
Simple pic
// Called by a GUI button press
// This will take a picture from the camera
function takePic()
{
// ConsoleFunction declared in source code
platformiPhoneTakePicture();
}But hopefully I can get to this:
Advanced pic
// Called by a GUI button press
// %type - string determining whether to take a screen shot
// or capture image from device camera
function takePic(%type)
{
if(%type $= "screenshot")
platformiPhoneTakeScreenshot();
else if(%type $= "camera");
platformiPhoneTakePicture();
}I won't think about rendering the actual image just yet. I think if I can get image capturing down, I can tackle rendering unless someone else will take that.
How does that plan of action sound?
#4
However, I have an iTGE game at my disposal that has this functionality. There is a GUI button that makes the keyboard pop up. I think I should grab that code, port it to iTGB, figure out how it works, then apply that to our camera code.
02/22/2009 (10:43 am)
One other thing. I'd have to check the forum threads, but I do not think anyone has got the iPhone keyboard to display in TGB.However, I have an iTGE game at my disposal that has this functionality. There is a GUI button that makes the keyboard pop up. I think I should grab that code, port it to iTGB, figure out how it works, then apply that to our camera code.
#5
The way you described it before (I think) was that by taking a screenshot with the phone you create an event that is sent to TGB that is caused by an input on the IphonePlatform. Now we are trying to get it to send us an event using an input on the TGB side.
So your GUI button would simulate whatever triggers the "screenshot" on the iPhone to get it to send an event to TGB? In logic if you could get it to do that it seems like it should work.
If you can find how the Keyboard is called then it shouldn't be to hard to replace keyboard with the PhotoLibrary or the camera function. That might be an easy way to tie it all into your GUI Button.
02/22/2009 (11:16 am)
The script makes sense to me but the part that seems to be an issue is using the GUI to send an event to the iPhonePlatform telling it to take a picture then have it send that event back to TGB. The way you described it before (I think) was that by taking a screenshot with the phone you create an event that is sent to TGB that is caused by an input on the IphonePlatform. Now we are trying to get it to send us an event using an input on the TGB side.
So your GUI button would simulate whatever triggers the "screenshot" on the iPhone to get it to send an event to TGB? In logic if you could get it to do that it seems like it should work.
If you can find how the Keyboard is called then it shouldn't be to hard to replace keyboard with the PhotoLibrary or the camera function. That might be an easy way to tie it all into your GUI Button.
#6
02/22/2009 (11:25 am)
Also out of curiousity where did you find the declaration of the function platformiPhoneTakePicture();or did you input this into the source yourself? I have not come across anything in script that calls the camera yet.
#7
02/22/2009 (11:27 am)
That's just the name I have planned for the ConsoleFunction, but it has not been created yet.
#8
02/22/2009 (11:32 am)
LOL nevermind since you found it :D.
#9
there are some other spots too I think but will we need to go in and make changes to these inputs or are they ok?
Also the .getPtr8 I am assuming is a pointer, but where is it pointing to?
02/22/2009 (12:01 pm)
I was looking into the platformiPhone section of the source and it looks like in the iPhoneInput.mm the input bools for the keyboard has been commented out.//------------------------------------------------------------------------------
bool Input::enable()
{
Con::printf( "[]Input::enable." );
gInputEnabled = true;
if ( smManager && !smManager->isEnabled() )
return( smManager->enable() );
enableMouse();
//enableKeyboard();
return( gInputEnabled );
}
//------------------------------------------------------------------------------
void Input::disable()
{
Con::printf( "[]Input::disable." );
gInputEnabled = false;
if ( smManager && smManager->isEnabled() )
smManager->disable();
disableMouse();
//disableKeyboard();
}
//------------------------------------------------------------------------------
void Input::activate()
{
smActive = true;
enableMouse();
// enableKeyboard();
}
//------------------------------------------------------------------------------
void Input::deactivate()
{
//Con::printf( "[]Input::deactivate." );
deactivateMouse();
//deactivateKeyboard();
smActive = false;
}there are some other spots too I think but will we need to go in and make changes to these inputs or are they ok?
Also the .getPtr8 I am assuming is a pointer, but where is it pointing to?
#10
Display iPhone OS Keyboard - Solved.
I've deleted the duplicate posts here, but to answer your question the keyboard stuff you found above does not appear to have any impact on the code I discovered.
I have to take a break, eat some food and what not. Try to integrate the keyboard code I just posted and see if it works in iTGB. If it does, then we can come back to the camera code.
Please let me know how it works out. If it's good to go, it will be pushed into an official iTGB release.
02/22/2009 (12:31 pm)
OK! I've created a new (and complete) thread about the OS keyboard:Display iPhone OS Keyboard - Solved.
I've deleted the duplicate posts here, but to answer your question the keyboard stuff you found above does not appear to have any impact on the code I discovered.
I have to take a break, eat some food and what not. Try to integrate the keyboard code I just posted and see if it works in iTGB. If it does, then we can come back to the camera code.
Please let me know how it works out. If it's good to go, it will be pushed into an official iTGB release.
#11
Going to try to get the UIKit and add it then compile the newly added code.
02/22/2009 (1:16 pm)
Well I created the files and added them to the source folder of iTGB. I redirected the #includes to find the TextEntryController.mm and .h. but I am getting a compile error looking for the UIKit. Going to try to get the UIKit and add it then compile the newly added code.
#12
02/22/2009 (1:23 pm)
Can you post the compile error so we know what went wrong, in case others follow your steps?
#13
02/22/2009 (1:34 pm)
Well I got all the files needed. I got the UIKit from the iphoneSDK and also you need to grabe the CoreGraphics/CGGradient.h and CoreGraphics.h and add those to the build. Now I am just getting some C++ errors.
#14
Located in TextEntry.mm line 10.
02/22/2009 (1:39 pm)
Well there are 665errors while 90% of them are just repeated the source of the error seems to be this.Located in TextEntry.mm line 10.
error: 'NSUTF16LittleEndianStringEncoding' was not declared in this scope
#15
By themselves the compile fine, but when the TextEntryController and TextEntry try to utilize it a lot of declaration errors occur.
02/22/2009 (1:48 pm)
Hmm it apprears the errors are deriving from CoreGraphics and the UIKit.. Do you see a CoreGraphics folder and a UIKit folder in the iTGE?By themselves the compile fine, but when the TextEntryController and TextEntry try to utilize it a lot of declaration errors occur.
#16
1. Opened the engine/source folder
2. Created a folder called Wrappers
3. Added TextEntry.h and TextEntry.mm to the wrappers folder
4. Right clicked Torque2D_iPhone, add->existing files->wrappers folder
5. Created a folder called Classes
6. Added TextEntryController.h and .mm to Classes folder
7. Added folder and files to project as I did before
8. Created a folder called UI in the same directory as the Xcode project
9. Added .xib file
10. Added this code to GuiTextEditCtrl.cc, just below all the stock headers:
12. Build
13. Success
I haven't created the ConsoleFunction and tested yet, but at least it is compiling for me. I haven't added anything else. I'll test now and let you know how it goes. If it works, I'll send you the .xib file.
02/22/2009 (1:59 pm)
OK. Mine just compiled when I added the files. There is one thing I have that you do not, and that's a UI folder with a TextEntry.xib file. Here is what I did1. Opened the engine/source folder
2. Created a folder called Wrappers
3. Added TextEntry.h and TextEntry.mm to the wrappers folder
4. Right clicked Torque2D_iPhone, add->existing files->wrappers folder
5. Created a folder called Classes
6. Added TextEntryController.h and .mm to Classes folder
7. Added folder and files to project as I did before
8. Created a folder called UI in the same directory as the Xcode project
9. Added .xib file
10. Added this code to GuiTextEditCtrl.cc, just below all the stock headers:
#ifdef __IPHONE__ #include "wrappers/TextEntry.h" #endif11. Clean
12. Build
13. Success
I haven't created the ConsoleFunction and tested yet, but at least it is compiling for me. I haven't added anything else. I'll test now and let you know how it goes. If it works, I'll send you the .xib file.
#17
This is probably cause I haven't created the console function for the function?
When adding your console function I get a few errors for this section.
The errors I get are...
02/22/2009 (2:25 pm)
Well I got rid of the other errors by removing what I did and changed to what you got. I have one error now."TextEntry::getUserText(StringBuffer&)", referenced from: GuiTextEditCtrl::onMouseUp(GuiEvent const&) in guiTextEditCtrl.o ld: symbol(s) not found collect2: ld returned 1 exit status Build failed (1 error)
This is probably cause I haven't created the console function for the function?
When adding your console function I get a few errors for this section.
StringBuffer mTextBuffer; TextEntry::getUserText(mTextBuffer) char *ret = Con::getReturnBuffer(mTextBuffer + 1); return ret; }
The errors I get are...
error: 'StringBuffer' does not name a type
error: 'TextEntry' has not been declared
error: expected constructor, destructor, or type conversion before '(' token
error: expected unqualified-id before 'return'
error: expected declaration before '}' token
#18
I no absolutely nothing about Interface Builder, but at least the code works. Again, I had 0 errors. I'll post the files I use in the other thread. Let's continue our discussion there. Don't want this valuable information lost in this thread.
02/22/2009 (3:02 pm)
Hmmm...Just tested mine and it works perfectly. The one problem is the interface file (.xib) I borrowed from iTGE is vertical only. The keyboard displays properly, but the input box with "commit" and "cancel" does not display well in a landscape scene.I no absolutely nothing about Interface Builder, but at least the code works. Again, I had 0 errors. I'll post the files I use in the other thread. Let's continue our discussion there. Don't want this valuable information lost in this thread.
#19
Did you make any changes to the function you posted earlier?? I have edit it slightly and it should work but for some reason it isn't recognizing the + operator.... If I copy your function directly it gives those 5 errors listed above.
*got a headache now* =). Something so simple and I don't know why this isn't working.
02/22/2009 (4:40 pm)
Hmm I am still messing with the ConsoleFunction.. You added the function to guiTextEditCtrl.cc? Did you make any changes to the function you posted earlier?? I have edit it slightly and it should work but for some reason it isn't recognizing the + operator.... If I copy your function directly it gives those 5 errors listed above.
*got a headache now* =). Something so simple and I don't know why this isn't working.
#20
1. Create a directory called "ui" in the same folder as your Xcode project
2. Copy the above file into that directory
3. Add the "ui" directory to the resources group of your Xcode project
02/22/2009 (5:35 pm)
TextEntry.xib file1. Create a directory called "ui" in the same folder as your Xcode project
2. Copy the above file into that directory
3. Add the "ui" directory to the resources group of your Xcode project
Community Manager Michael Perry
ZombieShortbus