Use image with iText and AbstractPdfView - image

I'm using iText for creating a PDF with the AbstractPdfView.
My image is located under
-webapp
--resources
---img
----logo.jpg
I'm trying to load this into my PDF but I always get a FileNotFoundException
Image.getInstance("/resources/img/logo.jpg")
How do I load an image which is located under my webapp folder into my PDF?

Was just doing that this AM...
ServletContext servletContext = request.getSession().getServletContext();
Resource res = new ServletContextResource(servletContext,"/images/logo.png");

First of all add a folder inside src/main/resources. For example:
src/main/resources/images
Inside this folder put your image. Let's say:
src/main/resources/images/logo.png
Then you can use this resource from an AbstractPdfView as:
URL imageUrl = getClass().getResource("/images/logo.png");
Image logo = Image.getInstance(imageUrl);
Then use that image in your document however you need to.
Regards!

Related

Azure Storage Blob Container Vituval Folder Image Download IN JAVA

I want to download a image from Azure Storage Blob Container have virtual folder, it contain Image, need to download that image in java code.
Asure Storage Stucture:
"Blob-Container" -> "Blob-Folder" -> "Sample.jpg"
Below code is direct download image from Container, i need to download image from fodler inside the container
BlobServiceClient storageClient = new BlobServiceClientBuilder()
.endpoint(endpoint.toString())
.credential(credential)
.buildClient();
BlobContainerClient blobContainer = storageClient.getBlobContainerClient(azureContainer);
BlockBlobClient blobClient = blobContainer.getBlobClient(fileName).getBlockBlobClient();
blobClient.download(response.getOutputStream());
First of all, you need to know that the storage of azure blob storage is actually flat, it actually has no so-called folders at all.
What I mean is that you have to combine the path and file name as the filename passing method. If you list the files in the current container, you will find that they are in a form similar to this:
folder1/folder2/filename.suffix

Not a valid resource name of assembly

I'm using Xamarin Studio and GTK#(.NET). I have the following problem:
I've made an app, and I need to(dinamically) download web images(favicons) and display them in a Gtk.Image widget. When I try to do this, I get the following error:
System.ArgumentException: 'myfile.ico' is not a valid resource name of assembly 'myApp, Version=1.0.6742.24722, Culture=neutral, PublicKeyToken=null'.
at at Gdk.PixbufLoader.InitFromAssemblyResource(Assembly assembly, String resource)
at at Gdk.PixbufLoader..ctor(Assembly assembly, String resource)
at at Gdk.Pixbuf..ctor(Assembly assembly, String resource)
at at Gdk.Pixbuf.LoadFromResource(String resource)
It works if i add the favicon, after download,in the resource folder, BUT I need to make this dinamically(if I add a new site, I want to see it's icon there..).
The code:
using (WebClient client = new WebClient())
{
client.DownloadFile(new Uri("https://google.com/favicon.ico",
Directory.GetCurrentDirectory() + "\..\..\Res\google.ico");
}
faviconImage.Pixbuf = Gdk.Pixbuf.LoadFromResource("myApp.Res.myfile.ico");
The image is in the folder(i can see it, it's everything ok) but i cannot use it if i don't include it(manually) in the resource folder....
Please help me :'(
Pixbuf.LoadFromResource only loads image data from an embedded assembly resource. You can not add resources to assemblies after they are created.
To create a Pixbuf from a file, use one the its .ator that supports either a file path to the image or one that uses a file stream.
Something like:
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "myDownloadIcon.ico");
var newPixBuf = new PixBuf(filePath):
faviconImage.Pixbuf = newPixBuf;

Saving Images Using Spring Boot and MySQL

I am creating a web app using Spring Boot and JPA. I want to upload many images. I want those images to save in a storage and save the location of the file into database. I am not understanding how to achieve this?
After researching a lot, I have come to a solution. I have used Amazon AWS S3 for storing the media data. Amazon AWS S3 comes with its own library. It gives you rich api to upload the media files and having control over them. I have just uploaded the file into the S3 Bucket and saved the corresponding URL into the MYSQL database. Please comment if you need the corresponding codes.
I have encountered this problem in Django application.
What i did was just used amazon S3 bucket and store all the images. I get the location of the image which is in s3 and store it in mysql DB. when i need the image i just call the image from s3 and used it.
Accept Images as a zipstream(Faster)/binarystream or as a MultipartFile
Load the stream ZipInputStream zis = new ZipInputStream(inputStream);
load the stream to File using Java.IO.File class
Push the file data to file system(Same server machine or any non-sql Database)
save the Location in a DB using Spring data JPA
This is how I DID it.
I store my images as a LONGBLOB in my MySQL database so use columnDefinition = "LONGBLOB". In your HTML code for your upload button, make sure you include the multiple = "true" in your tag if you are using HTML.
In your Controller class make a multipart file that will get the images if you are using binding models and also, you need to have a Set/List of images so you can go through them all.
MultipartFile[] images = bindingModel.getImages();
Set<Image> newImages = new HashSet<>();
I have this neat for loop so I can go through all of my images:
for (MultipartFile multipartFileFOREACH : images)
{
Image newImage = new Image(multipartFileFOREACH.getBytes(), postEntity);//Image is an Entity here, postEntity is an object
newImages.add(newImage);
this.imageRepository.saveAndFlush(newImage);
}
this.articleRepository.saveAndFlush(articleEntity);
So, here I store my bytes into the LONGBLOB in the SQL database
Then for the actual view
Set<Image> imagesBytes = post.getImages();
List<String> images = new ArrayList<>();
for (Image image : imagesBytes)
{
images.add(Base64.getEncoder().encodeToString(image.getLink()));
}
My Image Entity has a link variable of type byte[]
Then I simply add the view for the HTML:
model.addAttribute("images", images);
use s3 bucket for storing image and my sql for storing the address of the image
Using MultipartFile you can upload the file.
Save the File in your server using Files.Copy through BufferedInputStream
Save the saved-location of your file in your database through Spring-Data repository.
There are three options to store your files.
a) in your local file system
b) store in db as bytes
c) use cloud service such as AWS S3 bucket
This is a good tutorial for setting up S3 bucket with SpringBoot applications.
https://medium.com/oril/uploading-files-to-aws-s3-bucket-using-spring-boot-483fcb6f8646

How to decode a bitmap from a local file deployed with Android application

I am going through http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/
and trying to get the Local Images working with Android however I am experiencing some issues when attempting to do something in normal monodroid just as a further test. I'm using a SharedProject, just for reference.
I have added the test image at Resources/drawable (test1.png), and also setting the Build Action as AndroidResource as it describes, and if I do the following in Xamarin.Forms it works:-
Xamarin.Forms.Image myimage = new Image();
myimage.Source = ImageSource.FromFile("test1.png");
However, if I try and retrieve the same file via the following it comes back as null.
Android.Graphics.Bitmap objBitmapImage = Android.Graphics.BitmapFactory.DecodeFile("test1.png")
Does anyone know why this is null and how to correct?
The Xamarin.Forms FileImageSource has a fallback mode as it covers two different scenarios.
First it will check whether the file exists in the file system via the BitmapFactory.DecodeFile.
Should the file not exist as specified in the File property, then it will use BitmapFactory.DecodeResource to try and load the resource content as a secondary alternative.
FileImageSource for Android therefore isn't only just for files that exist in the file system but also for retrieving named resources also.
This link indicates that Android Resources are compiled into the application and therefore wouldn't be part of the file system as physical files.
Since test1.png is a Drawable you should use BitmapFactory.DecodeResource ()
string scr = "#drawable/test1.png";
ImageView iv = new ImageView(context);
Bitmap bm = BitmapFactory.DecodeFile(src);
if (bm != null)
iv.SetImageBitmap(bm);

How to access resources in Java Struts 1 app

In a Struts 1 Web App I can access images from a .jsp by
<img src="../../images/myImageName.png"/>
Am I also able to reference that image from a .java class in my source directory?
I am using iText and can fetch an image from a url
String imageUrl = "http://jenkov.com/images/" +
"20081123-20081123-3E1W7902-small-portrait.jpg";
Image image2 = Image.getInstance(new URL(imageUrl));
but if I try and fetch one from
Image image = Image.getInstance("../../images/myImageName.png"/);
It always looks in the bin folder of my server. How can I get the relativePath back to my image?
If the image is in the web app content directory then you need the app context relative path:
String relativeWebPath = "images/myImageName.png";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
Would I do that? Not unless there's a good reason; I'd prefer either a path off the app context altogether (and the image could be streamed by the app, or set as a symlink, etc) or a classpath-based resource.

Resources