I'm a beginner in Unity. And so far of what I read, playerprefs could store image in a byte form. Still, I'm having trouble in this one. Because, I'm having an error in converting texture2d to bytes. In which, I use this code
Texture tex = Resources.Load("Sprites/white_black") as Texture2D;
byte[] texbyte = tex.EncodeToPNG();
// ^ this line always result as NullReferenceException on my console.
I have two scenes: Scene1 is images that were going to save to playerprefs which is an example below:
While in Scene 2, all the images that were saved in playerprefs will be show using a button like in below pic:
Also, if you could recommend me other solution I'll search on it. Thank you.
You should not save images in PlayerPrefs but you could just save the name of the sprite to PlayerPrefs, and then load it in another scene from resources, like so: Resources.Load(spriteName);
Related
I've been trying for two days to find an alternative to loading an image into my current project. I am using Adobe Flash Professional CS6 as my IDE and Animation program. I would like to be able to display an image in my application. What I am trying to do is have the image display onto the screen, the user enters the PLU associated with the image, and if the PLU is right then they receive a point. I have everything else already to go, but I just can't find an efficient way to deal with loading the image.
Right now I'm using this to accomplish getting my image on the display:
var myDisp:Layer0 = new Layer0();
var bmp:Bitmap = new Bitmap(myDisp);
spDispBox.addChild(bmp);
The above code works just find, but the limitation I can't get around is that I'm going to have to import each image into the library and then consecutively code each part in. I wanted to stick to OOP and streamline this process, I just don't know where I should turn to in order to accomplish my project goal. I'm more than happy to give more information. Thanks in advance, everyone.
July, 26, 2014 - Update: I agree, now, that XML is the way to go. I'm just having a hard time getting the grasp of loading an external XML file. I'm following along, but still not quite getting the idea. I understand about creating a new XML data object, Loader, and URLRequest. It's just loading the picture. I've been able to get output by using trace in the function to see that the XML is loaded, but when I go to add the XML data object to the stage I'm getting a null object reference.
I'm going to try a few more things, I just wanted to update the situation. Thanks again everyone.
it seems like these images are in your FLA library. To simplify your code you can make a singleton class which you can name ImageFactory (factory design pattern) and call that when needing an image which will return a Sprite (lighter than a MovieClip)
spDispBox.addChild( ImageFactory.getImageA() ); // returns a Sprite with your image
and in your ImageFactory
public function getImageA():DisplayObject {
var image:Layer0 = new Layer0(); // image from the FLA library
var holder:Sprite = new Sprite();
holder.addChild( new Bitmap( image ) );
return holder;
}
also recommend using a more descriptive name than Layer0
I have a card game program that when a card gets discarded and is found to be a certain type of card, it goes into my DefeatedChars pile, which is just a PictureBox named DefeatedChars. Currently all my images are loaded during runtime using ImageLocation which is the string of some website that stores all the card images. I want to know how to load this string to the PictureBox's Image property so I can rotate it because trying to rotate gives me a null pointer exception because Image is nothing, at least I think that's why. I do know through debugging that when DefeatedChars.Image is nothing. My question is, is there a way to rotate the ImageLocation after it's been loaded or is there a way to move the image found at the ImageLocation into the image property. Ultimately, when I'm done with the game, I want the image found at the image location to be stored somewhere locally so that if the website changes, which it does, the cards will retain their images and properties, but that's a question for later down the road. Here is the code I am stuck at that gives me a null pointer exception. I'm coding in Visual Studio.
DefeatedChars.ImageLocation = tempCard.ImageLocation
DefeatedChars.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
I tried this...
DefeatedChars.Image = Image.FromFile(tempCard.ImageLocation)
But I ran into a "URI formats are not supported error". Then I found this little gem on the internet, which replaces the first line of code above.
DefeatedChars.Image = Image.FromStream(System.Net.HttpWebRequest.Create(tempCard.ImageLocation).GetResponse().GetResponseStream())
It turns the URL into a stream so it can be used by the Image property and it works like a charm.
I am trying to use Galleria where I am dynamically inserting images by calling the push() API. That part is working just great when it comes to pushing a new image in the carousel. However, what I want to do is as soon as I push a new image, I would like to load that image in the main stage to display the large version. I can't figure out how to do that and am hoping someone here can help.
Here is what I have tried so far. I thought after pushing a new image I can call the getDataLength(). This would potentially return either original length or (original length + 1) if it accounted for the new pushed image. getDataLength() returns the original length. So I tried to use show(original length + 1) hoping that would load the new image. However, this simply revolves around and shows the first image in the carousel instead of loading the last newly pushed image.
Any help would be greatly appreciated.
Thanks.
Seen a good charts plugin I want to use:
http://www.jqplot.com/tests/stackedTests.php
But for my visitors, they may want to save this as an image (via right click, copy as image menu item) or alternatively a button that says save as image or something similar.
So is it possible to save the canvas in any given state as an image file?
I think toDataURL could help you :)
If you are willing to use a pre-made library, you can try using Canvas2Image.
Otherwise there are a few Canvas methods that Canvas2Image wraps around and explain in more detail in the above site, namely the toDataURL method in the Canvas object that returns the data base64 encoded in the image format that you require. It's not 100% cross-browser, I think, but it's the "right" way of getting it.
Is it possible to output images so that they all will be inside a single window?
Before, I used to output data using only opencv functions:
cvNamedWindow("Image 1");
cvShowImage("Image 1", img);
So I change image, then call: cvShowImage function and so on.
But If I want to look at more than one image, then every new image needs its own window to be shown there And what I want is to put every such an output opencv's window inside one big main window.
Is it possible to do it? And how?
You will have to construct a new image and place each img into it. I don't think there's a builtin function like MATLAB's subplot. I recommend using the ROI functions to quickly copy an image into a region-of-interest (ROI) of the big image (which holds the others).
You can show as many images as you want on a single window using hconcat function.
Let us suppose your original image was
Mat frame;
Now clone or make a copy of this image using
Mat frame1 = frame.clone();//or
Mat frame2;
frame.copyTo(frame1);
Now let us suppose your output images are
Mat img1,img2,img3,img4;
Now if you want to show images horizontally, use
hconcat(img1,img2,frame1)//hconcat(input_image1,input_image2,destination_image);
And if you want to show images vertically, use
frame2.push_back(img1);//main_image.push_back(image_to_be_shown_below);
This process processess images one at a time, so if you want to show 4 images side by side, you have to call this function 4 times as in
hconcat(img1,img2,frame1);
hconcat(frame1,img3,frame1);
hconcat(frame1,img4,frame1);
imshow("Final Image",frame1);
NOTE:
Clone process is done because the images have to be of same size.
Enjoy...