Re-write file without EXIF data in Android Studio - apache-commons

I want the java code or library to write the minimal EXIF data back to IMAGE files (JPG, GIF, PNG, etc.) in Android Studio. The EXIFREWRITER is not working. Also, METADATAEXTRACTOR is extracting but not re-writing back to the file. Thanks!

Below code worked for me for converting all extensions (HEIC, GIF, CR2, PNG etc.):
FileInputStream inStream = new FileInputStream(new File(inputFilePath));
Metadata metadata = ImageMetadataReader.readMetadata(inStream);
Bitmap picBitmap = BitmapFactory.decodeFile(inputFilePath);
FileOutputStream outStream = new FileOutputStream(new File(copiedFile));
picBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println("Tag :" + tag);
}
}

Related

Need to dispaly svg on an image in Xamarin

I have an url that gets team logos but it returns svg https://www.mlbstatic.com/team-logos/141.svg.
How can i display this in a Image for xamarin forms?
Searched and only found complex huge amounts of code.
looking for
Download image -- I have this but what do i need to save it in GetResponsestream preferrable i would like to stay in memory and not write to disk or file.
Attach it to an image to display.
Thanks.
Ok, thought i would post my solution here.
I used SkiSharp:
SkiaSharp.Extended.Svg.SKSvg svg = new SkiaSharp.Extended.Svg.SKSvg();
using (WebClient client = new WebClient())
{
// ie for theurl: https://www.mlbstatic.com/team-logos/141.svg
svg.Load(new MemoryStream(client.DownloadData(new Uri(theurl))));
var bitmap = new SKBitmap((int)svg.CanvasSize.Width, (int)svg.CanvasSize.Height);
var canvas = new SKCanvas(bitmap);
canvas.DrawPicture(svg.Picture);
canvas.Flush();
canvas.Save();
string filename = "";
using (var image = SKImage.FromBitmap(bitmap))
using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
{
// save the data to a stream
filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp.png");
using (var stream = File.OpenWrite(filename ))
{
data.SaveTo(stream);
}
}
}
use FileName from above to assign source to Xamarin image.
this accomplished the task with the least amount of code lines i tried.

GET TEXT FROM IMAGE EMBEDDED IN A .docx FILE USING TIKA

I've been working on Text Extractor that works on .docx file using Tika. And it is working file for basic text and text in tables and textboxes, but it fails for images.
How do I get text from Image, tesseract along with tika can be used to get text from an image alone but for that I would need to extract out the image from document. How do I do this?
Kindly help if anybody has worked upon something like this.
This the code that works fine for text, textbox and tables,but not for images:
public class BasicDocumentExtractor {
public static void main(final String[] args) throws IOException,SAXException, TikaException {
//detecting the file type
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream=new FileInputStream(new File("D:\\Nidhi\\sw\\ws\\Hello.docx"));
ParseContext pcontext=new ParseContext();
//OOXml parser
OOXMLParser msofficeparser=new OOXMLParser ();
msofficeparser.parse(inputstream, handler,metadata,pcontext);
System.out.println("Contents of the document:" +handler.toString());
/*System.out.println("Metadata of the document:");
String[] metadataNames = metadata.names();
for(String name : metadataNames){
System.out.println(name + ": " + metadata.get(name));
}*/
}
}
You need to enable recursion in Tika in order to get the embedded images. The simplest way is normally just to use the RecursiveParserWrapper to do it for you.
If you use it, your code would instead be roughly
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
TikaInputStream input = TikaInputStream.get(new File("D:\\Nidhi\\sw\\ws\\Hello.docx"));
Parser wrapped = new AutoDetectParser();
RecursiveParserWrapper wrapper = new RecursiveParserWrapper(wrapped,
new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.TEXT, 60));
wrapper.parse(stream, handler, metadata, context);
// Get metadata from children
List<Metadata> list = wrapper.getMetadata();
// Get metadata from main document
System.out.println("Main doc name is " + metadata.get(TikaCoreProperties.TITLE));
System.out.println("Contents of the document:" +handler.String());
As I was trying really hard to do this since las 24hours, I figured out a way, a pretty easy one. Since, Tika is built on the top of POI, using POI this task can be efficiently executed. Also, it is not a direct solution so alomost no tutorials are available for this purpose, I hope nobody else has to face this issue in future. This is the running code that extracts all images from a .docx document:
public static void getImages() throws Exception {
XWPFDocument doc=new XWPFDocument(new FileInputStream("D:\\Nidhi\\CDAC\\Images\\test1.docx"));
List images=doc.getAllPictures();
int i =0;
while (i<images.size()) {
XWPFPictureData pic= (XWPFPictureData) images.get(i);
System.out.println(pic.getFileName() + " "+ pic.getPictureType() +" "+ pic.getData());
FileOutputStream fos=new FileOutputStream("D:\\Nidhi\\CDAC\\Images\\b" + i+".jpg");
fos.write(pic.getData());
i++;
}
}
Also, if it will work on all MS Office 2007+ files, for .doc and such files use HWPF in the exactly same manner.

Exception when open excel: File contains corrupted data

I am trying to read an excel with OpenXML.
What I did is simply as following:
private WorkbookPart wbPart = null;
private SpreadsheetDocument document = null;
public byte[] GetExcelReport()
{
byte[] original = File.ReadAllBytes(this.originalFilename);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(original, 0, original.Length);
using (SpreadsheetDocument excel = SpreadsheetDocument.Open(stream, true))
{
this.document = excel;
this.wbPart = document.WorkbookPart;
UpdateValue();
}
stream.Seek(0, SeekOrigin.Begin);
byte[] data = stream.ToArray();
return data;
}
}
I initialized this.originalFilename in the constructor. It is the filename ended with '.xlsx' which i created with excel 2010.
But this line of code
using (SpreadsheetDocument excel = SpreadsheetDocument.Open(stream, true))
gives the exception: Message: System.IO.FileFormatException: File contains corrupted data.
The StackTrace:
Does anyone know how to solve this problem? At the beginning, I didn't use the Stream, I just use SpreadsheetDocument.Open(filename, true). However, it turns out to be exactly the same exception.
I've tried to create a new .xlsx file, but it's still the same.
There is a MSDN page which describes the process of reading and writing Excel file using stream and open xml SDK.
http://msdn.microsoft.com/en-us/library/office/ff478410.aspx
Try extracting the document contents through zip application and check whether you are getting the standard folders inside like xl,docProps and _rels etc.,
This is a method to find whether the package is properly packaged as archive or not.
Hope this helps.

Vaadin: Opening a new window with PDF content placed in a String variable

I'd like to open a new window with PDF content that is placed within a String variable.
I already have a button with an event connected. In this event I want to call the new window.
The method looks like this:
private void show_archivobjekt(String data) {
String pdf = anfrage.get_archivobjectdata(data);
System.out.println(pdf); // This shows my PDF content in console and works!
// How to convert this String into a StreamSource
StreamResource streamResource = new StreamResource(pdfss, "test.pdf", myView);
streamResource.setCacheTime(5000);
streamResource.setMIMEType("application/pdf");
myView.getMainWindow().open(streamResource, "_blank");
}
myView is the Application.
How can I convert the String pdf to a StreamSource (pdfss)? Do I have to save it as file at first or is it possible to convert it to a StreamSource directly in memory?
The console output shows me the typically PDF content starting with %PDF-1.3 ... and so on.
Any help would be appreciated. Thanks in advance!
Rainer
The answer to this question is available through the official support forum here: https://vaadin.com/forum#!/thread/148544
Simply create your StreamResource like this, by using the byte representation of your string to create a ByteArrayInputStream as the source:
StreamResource streamResource = new StreamResource(
new StreamResource.StreamSource() {
public InputStream getStream() {
return new ByteArrayInputStream(pdf.getBytes());
}
}, "test.pdf");

How to programmatically create an InfoPath form from an InfoPath XSN template

I need a solution that creates an InfoPath instance form from an XSN template that exists on a SharePoint server, I am using this approach but this extracts template files on temp directory of server that we may not have write permission to. Is there better solution for this?
You just change the CAB-library, to one that can extract the template file to memory, as this one,
Minimum C# code to extract from .CAB archives or InfoPath XSN files, in memory
And then call, myCab.ExtractFile("template.xml", out buffer, out bufferLen);
the complete code would look something like
private byte[] GetXmlForm(SPDocumentLibrary list) {
byte[] data = null;
SPFile file = list.ParentWeb.GetFile(list.DocumentTemplateUrl);
Stream fs = file.OpenBinaryStream();
try {
data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
} finally {
fs.Close();
}
byte[] buffer;
int bufferLen;
CabExtract cab = new CabExtract(data);
cab.ExtractFile("template.xml", out buffer, out bufferLen);
return buffer;
}

Resources