Save image in javaFX with applying effects on the image - image

I am making an application in JavaFX where the user can upload the photo and apply the effects on it, and in the end, save the image with the effects.
When I save the image, the original image is saved without the effects.
I get the image from the ImagView and save it in the file but the image is saved without the effects.
How I can solve this problem and have the picture with the effects that I applied to it?
The methods below the first one is to apply the effects and the second one is to save the image from ImageView
I need any idea to help me to solve this problem please.
\\Method of applying the filter
public void applyFilter(){
SepiaTone sepiaTone = new SepiaTone(cons);
myImgView.setEffect(sepiaTone);
}
public void saveImage() throws IOException {
Image imageToBeSaved = myImgView.getImage();// get the image from ImageView
File file = new File("D:/image.png");
try {
ImageIO.write (SwingFXUtils.fromFXImage(imageToBeSaved,null), "png", file);
}
catch (IOException e) {
e.printStackTrace();
}
}

Related

XAML Image source has issues displaying a deep nested path

This is quite vexing.
I am working on an app for image management. Part of the value is the ability to store images in sub-folders based on image properties, eg. creation date.
If I store the image source in a shallow folder (app\images\img.jpg), everything works fine.
If I store the image in KnownFolders.Pictures\source\year\month\day\img.jpg, Image does not render. (Yes, that specific path won't work, I am trying to give you a sense of how the path is constructed)...
The file is actually there. The path is correct (I can open it in a browser, e.g.). The app has access to the file.
But it does not render the bitmap.
I tried to render the bitmap manually using
new BitmapImage(new Uri("KnownFolders.Pictures\source\year\month\day\img.jpg"),UriKind.Absolute))
That does not render anything. (Again, assume the path is valid and has a file at its bottom).
What Am I Missing?
The head scratcher: for GIF anims, I am using Thomas Levesque's useful component: https://github.com/XamlAnimatedGif. That one, unfortunately, does only render gifs... and it does so even when the path is the one given above. So the Standard IMAGE control does not render correctly, but Thomas's control does... infuriating.
An UWP app can't load a BitmapImage from an absolute URL to a file in a folder structure below the Pictures Library Folder.
So this won't work:
var relativePath = #"source\year\month\day\img.jpg";
var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(relativePath);
var bitmapImage = new BitmapImage(new Uri(imageFile.Path));
However, you could do this:
var relativePath= #"source\year\month\day\img.jpg";
var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(relativePath);
var bitmapImage = new BitmapImage();
using (var stream = await imageFile.OpenAsync(FileAccessMode.Read))
{
await bitmapImage.SetSourceAsync(stream);
}
So, after way too much time spent on this...
First, link to DataContextChanged of the IMAGE element. In there, parse the DataContext out. If you are using the IMAGE outside of an ItemsControl etc, this is not required...
private async void ImageView_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (sender is Image)
{
Image img = (Image)sender;
if (img.DataContext is ImageView)
{
MyViewDataContext dc = (MyViewDataContext)img.DataContext;
img.Source = await dc.bitmap();
}
}
}
And here the implementation of MyViewDataContext.bitmap() which has a property called source that yields, you guessed it, absolute paths:
public async Task<BitmapImage> MyViewDataContext.bitmap()
{
if (_bitmap == null)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(source);
bool r = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.CheckAccess(file);
if (r)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// create a new bitmap, coz the old one must be done for...
_bitmap = new BitmapImage();
// And get that bitmap sucked in from stream.
await _bitmap.SetSourceAsync(fileStream);
}
}
}
catch (Exception e)
{
_bitmap = null;
}
}
return _bitmap;
}
BitmapImage _bitmap;
I cache the resulting bitmap until I dispose of this MyViewDataContext.
I am now most concerned about memory. This one worries me:
How to dispose BitmapImage cache?
So, as a tech debt, I am going to address the potential mem leaks later, once this whole thing is on the test bench and I can take a look at its runtime behavior...
To access the folders and libraries represented by the properties of this class, specify the corresponding capabilities in your app manifest. For example, to access KnownFolders.PicturesLibrary, specify the Pictures Library capability in the app manifest.
Hope this will help
KnowFolders

WP7: Get image's name which images are stored in Libary picture?

I have some images that are stored in Media Libary, view here. Now I need to get the name of an image after I choose the image by using photo chooser task. I used photo chooser task to select an image and then got the path of the image. My pupose is get the name from the path:
private void button1_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooseCall);
objPhotoChooser.Show();
}
void PhotoChooseCall(object sender, PhotoResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
BinaryReader objReader = new BinaryReader(e.ChosenPhoto);
image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
MessageBox.Show("Photo's name: " + e.OriginalFileName.ToString());
break;
case TaskResult.Cancel:
MessageBox.Show("Cancelled");
break;
case TaskResult.None:
MessageBox.Show("Nothing Entered");
break;
}
}
Output:
Photo's name: \Applications\Data\C80566AB-E17E-495C-81A1-3FCAE34D3DEDE\Data\PlatformData\PhotoChooser-a8208960-3597-40fc-9b4f-869afcf822b6.jpg
After I choose the same image. The name of it will change (PhotoChooser-a8208960-3597-40fc-9b4f-869afcf822b6.jpg will change). I think it's not the name of the photo.
So:
Can we get the name of the image?
And how do we do?
You can get the actual file names. Use the MediaLibrary for that:
MediaLibrary lib = new MediaLibrary();
var collection = lib.RootPictureAlbum;
foreach (var p in collection.Albums[0].Pictures)
Debug.WriteLine(p.Name);
Notice that I am specifying the album index inside the root picture album. 0 will be for sample pictures, and so on. If you need to grab the contents of the image, just use p.GetImage(); to get the readable stream.
No. You cannot get the name of the file as it is in the pictures hub. You are given a copy of the stream and that has a different, temporary, name.

Replace Image with out user noticing

I'm using JSF 2.1.7 + Prime Faces + Prime Faces Mobile.
I have a page that references a image:
<img src="/charts/chart1.png"/>
There's a separate background process that runs to update chart1.png every 2 minutes. (It gets the image from a particular online url). Here is the code I use to save the image:
public static String saveImage(final String strurl, final String imageFileName){
//assume the url for the image is valid
try {
URL url = new URL(strurl);
HttpURLConnection conn = (HttpURLConnection )url.openConnection();
conn.setConnectTimeout(1000); //1 sec timeout
conn.connect();
File file = new File(imageFileName);
BufferedImage image = ImageIO.read(conn.getInputStream());
ImageIO.write(image, "png", file);
return file.getAbsolutePath();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
ImageIO.write will delete the existing image file and write out a new one. I believe the above will result in a few milliseconds where the image file is not available, and hence the web user will not be able to see the image.
My questions are:
1) Is it true that there will be a time where the image is temporarily unavailable?
2) And if that is the case, what is a possible solution to fix this, so that user will always have a available image?
Thanks in advance. =)
Heres what i would do
1) First instead of replacing the image, i create a new image chart2.png
2)Then I would change the url in the IMG tag to point to a Servlet,
say
and in that servlet which will be mapped to /charts/showchart
,
And in the servlet i would check for latest available PNG , and stream it as response.
This way you customize how or what image you show.

AForge.NET Image Color Manipulation

I discovered AForge a few days ago with a goal in mind. I wanted to be able to manipulate an image's colors. However, after trying several different methods I have not been able to find a resolution.
I looked thoroughly through the documentation they give, but it hasn't been any help to me. The specific part of the documentation I have been using is:
http://www.aforgenet.com/framework/docs/html/3aaa490f-8dbe-f179-f64b-eedd0b9d34ac.htm
The example they give:
// create filter
YCbCrLinear filter = new YCbCrLinear( );
// configure the filter
filter.InCb = new Range( -0.276f, 0.163f );
filter.InCr = new Range( -0.202f, 0.500f );
// apply the filter
filter.ApplyInPlace( image );
I replicated it for a button click event, but the 'image' portion of it wasn't specified. I converted the image inside of my picturebox to a bitmap, then referenced it in the last line thinking that it would work. But it had no affect at all.
My code is the following:
private void ColManButton_Click(object sender, EventArgs e)
{
Bitmap newimage = new Bitmap(pictureBox1.Image);
YCbCrLinear filter = new YCbCrLinear();
filter.InCb = new Range(-0.276f, 0.163f);
filter.InCr = new Range(-0.202f, 0.500f);
filter.ApplyInPlace(newimage);
}
My question essentially is, to anyone familiar or willing to help with this framework, how do I take my image and manipulate its color using AForge's YCbCrLinear Class under my button's click event?
Remember to set the picture box image after you have applied the filtering.
private void ColManButton_Click(object sender, EventArgs e)
{
Bitmap newimage = new Bitmap(pictureBox1.Image);
YCbCrLinear filter = new YCbCrLinear();
filter.InCb = new Range(-0.276f, 0.163f);
filter.InCr = new Range(-0.202f, 0.500f);
filter.ApplyInPlace(newimage);
pictureBox1.Image = newimage;
}
at the aforge website you can download the source code of a sample filter application, did you try it ?

DataBound controls loading images and avoiding image cache in WP7

I want to load images into a Pivot header to substitute the lack of a Gallery control in WP7. I'm trying to populate them from a URL, and want to make sure that the image is not kept in the cache (by setting UriSource = null) to make sure that they don't take too much resources.
There's no way to do this in the XAML itself, can someone give me sample code to handle this from code-behind. my attempts have been unsuccessful. what am I doing wrong here?
public class PhotoGalleryVM
{
public ObservableCollection<BitmapImage> Images
{
get
{
ObservableCollection<BitmapImage> list = new ObservableCollection<BitmapImage>();
foreach (RoomImage r in App.appData.currentChoices.roomImages)
{
BitmapImage img = new BitmapImage(new Uri(Uri.UnescapeDataString(r.largeUri)));
img.UriSource = null;
list.Add(img);
}
return list;
}
}
}
There is an option that enables to ignore image cache:
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
Read more at msdn

Resources