How do you assign a default Image to the Blob object in Play framework? - file-upload

I followed this nice article
http://www.lunatech-research.com/playframework-file-upload-blob
and have a perfectly working image upload solution
My questions is, if the user doesn't select any image, how do I assign a default image during save (probably stored in the server)?
if (!user.photo)
user.photo= ?;
user.save();
The one-hack that I can think of is upload the default image and see which UID "Play" stores in the /tmp directory and assign that above. Is there an elegant named* solution to this?
when I say named, I mean I want the code to look like (which means I know what I'm doing and I can also write elegant automated code if there are more than one picture)
user.photo= "images/default/male.jpg"
rather than (which means I'm just hacking and I can't extend it elegantly for a list of pictures)
user.photo= "c0109891-8c9f-4b8e-a533-62341e713a21"
Thanks in advance

The approach I have always taken is to not change the model for empty images, but instead do something in the view to show a default image, if the image does not exist. This I think is a better approach because your are corrupting your model for display purposes, which is bad practice (as you may want to be able to see all those who have not selected an image, for example).
To achieve this, in your view you can simply use the exists() method on the Blob field. The code would look like
#{if user.photo.exists()}
<img src="#{userPhoto(user.id)}">
#{/if}
#{else}
<img src="#{'public/images/defaultUserImage.jpg'}">
#{/else}
I have assumed in the above code that you are rendering the image using the action userPhoto as described in the Lunatech article.

I'd assume you can store the default image somewhere in your applications source folder and use
user.photo.set(new FileInputStream(photo), MimeTypes.getContentType(photo.getName()));
to save the data. Photo is just a File object, so you can get the reference of your default image and use it.

Related

In gym, how should we implement the environment's render method so that Monitor's produced videos are not black?

How are we supposed to implement the environment's render method in gym, so that Monitor's produced videos are not black (as they appear to me right now)? Or, alternatively, in which circumstances would those videos be black?
To give more context, I was trying to use the gym's wrapper Monitor. This wrapper writes (every once in a while, how often exactly?) to a folder some .json files and an .mp4 file, which I suppose represents the trajectory followed by the agent (which trajectory exactly?). How is this .mp4 file generated? I suppose it's generated from what is returned by the render method. In my specific case, I am using a simple custom environment (i.e. a very simple grid world/maze), where I return a NumPy array that represents my environment's current state (or observation). However, the produced .mp4 files are black, while the array clearly is not black (because I am also printing it with matplotlib's imshow). So, maybe Monitor doesn't produce those videos from the render method's return value. So, how exactly does Monitor produce those videos?
(In general, how should we implement render, so that we can produce nice animations of our environments? Of course, the answer to this question depends also on the type of environment, but I would like to have some guidance)
This might not be an exhaustive answer, but here's how I did.
First I added rgb_array to the render.modes list in the metadata dictionary at the beginning of the class.
If you don't have such a thing, add the dictionary, like this:
class myEnv(gym.Env):
""" blah blah blah """
metadata = {'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': 2 }
...
You can change the desired framerate of course, I don't know if every framerate will work though.
Then I changed my render method. According to the input parameter mode, if it is rgb_array it returns a three dimensional numpy array, that is just a 'numpyed' PIL.Image() (np.asarray(im), with im being a PIL.Image()).
If mode is human, just print the image or do something to show your environment in the way you like it.
As an example, my code is
def render(self, mode='human', close=False):
# Render the environment to the screen
im = <obtain image from env>
if mode == 'human':
plt.imshow(np.asarray(im))
plt.axis('off')
elif mode == 'rgb_array':
return np.asarray(im)
So basically return an rgb matrix.
Looking at the gym source code, it seems there are other ways that work, but I'm not an expert in video rendering so for those other way I can't help.
Regarding your question "how often exactly [are the videos saved]?", I can point you to this link that helped me for that.
As a final side note, video saving with a gym Monitor wrapper does not work for a mis-indentation (as of today 30/12/20, gym version 0.18.0), if you want to solve it do like this guy did.
(I'm sorry if my English sometimes felt weird, feel free to harshly correct me)

Resizing in Dropzone.js?

Is there an example of the resize function for dropzone.js? I don't really understand how it works, it says:
"Resize is the function that gets called to create the resize information. It gets the file as first parameter and must return an object with srcX, srcY, srcWidth and srcHeight and the same for trg*. Those values are going to be used by ctx.drawImage()."
But I don't really get how to use it. So far I'm resizing the images on server-side, but I'd like to do it client-side and I think this might help. Any other solutions using dropzone.js if not this one?
I believe the built-in resize function in dropzoneJS is what is used to create the thumbnail, not resize the photo client-side, per se. There might be some way you could leverage it by setting the thumbnail dimensions to what you want to save to the server, and overwriting the file being saved with the thumbnail, but I'd have to hack about for a spell to offer you any code suggestions for that.

Showing past entries on UITextField

I am writing an app in which I have two UITextFields...
Starting Text
Destination Text
Now once I place values and hit the search or whatever function I want to call, I Want to reuse these values. The app should record and save these values as cache. And should show them when typing or upon a button click. Is that possible to show them just like Dictionary words show up or Which is more preferable tableView or PickerView? If there is any other please let me know.
Definitely use a UITableView. A UIPicherView is used modally most of the time and not for optional suggestions.
Table view is used in Safari, and a lots of other apps:
As for how to cache the data, you have lots of options. It also depends on how much data you expect.
One easy way would be to simply use NSArray. You can very easily write an NSArray to disk in a plist file and read it back when you need it.
Or you could use Core Data, if you expect lots of data and still want high performance. It will be a lot more difficult though to get used to that API if you've never tried it before. Basically you'll need a simple model with one entity called something like SearchEntry that has a single property text. Then you keep adding new instances to your managed object context and can easily filter the existing values.

Previewg images via IKImage View

I need help ,I am pressed by time and I am blocked. Ihave a table view containing a directory's files (icon, name, size, date) . Tha table is filled via an arraycontroller. The table view just displays the image files and pdf files.When I select a row I want to preview the content in an IKImage view which isjust under the table. But I can't preview the content. Can you help me and give me some documentation , tutorials or commands on how to preview them and how to use ikimage view? I guess the ikimage view is the best solution. What do you think about that?
I guess that you have come up with the solution but a referral for others who might come here.
read apple example
its simple to start with.
In your case you can use - (void)setImage:(CGImageRef)image imageProperties:(NSDictionary *)metaData where your file properties can go in as dictionary object.
In the apple's example you can check out how to get CGImageRef from filepath, or you can use setImageWithURL.

IKImageBrowserView Thumbnail Problem

I download IKImageBrowserView sample named "IKImageKit Demo" from Apple official website.
But I find a problem.
For example:
When I import images from folder named "A", then I delete an image.
Then I import other image (name as same as the deleted image), the thumbnail in
IKImageBrowser will not change.
How could I resolve this problem?? Thanks!
Video display: http://www.youtube.com/watch?v=f3Jue9wOdUI
I came across a reference that said the internal cache IKImageBrowserView uses looks up thumbnails by ImageUID. So if you change the ImageUID that your data source returns to be something other than just file name (maybe file name + file size), then it will be treated as a different image.
You should take a look at the IKImageBrowserItem Protocol's imageVersion method. If you need to let IKImageBrowserView know that a item has changed its image you can increment the imageVersion. This will force the view to update the cached image.
Update: Wow, old post. Looks like the documentation was updated a while after I had posted; as the other answers suggest, looks like you need to increment the image's imageVersion to update an image from cache.
After mulling over the problem for close to three hours (once I started, it was really bugging me, too!), I came to the sad conclusion that it's a bug in how IKImageKit handles images. IKImageBrowserView has a cache of images and thumbnails, and from whatever experimental data I have, I'm guessing that it just caches its images based on location on disk, and not on image data (so it can load the data lazily, I guess), so even if the actual image data can change, reloading the same file location will not update the image itself.
Of course, I could be completely wrong - which wouldn't surprise me, since there's literally no documentation on the issue, so I have nothing but results from Instruments and the app itself to guide me - but my advice to you is to ignore the issue for now, since there's nothing you can do about it, and possibly file the bug with Apple.
Of course, if anyone can introduce other evidence about this, please do! I'd love to know more about it.
Good luck!
you should want to imageVersion update or name the image of the path.
if you delete image with name "1.png" after you put image if it's name "1.png" the image browser view show the previous image so you should take care of image naming. or imageVersion method.