How to convert image to html Aspose.Words for Android via Java 24.6 in android java

when convert image to html face some issue

private void saveImageAsHtml(Uri imageUri) {
        try {
            // Convert URI to a file path
            ContentResolver contentResolver = getContentResolver();

            // Open an InputStream using the URI
            InputStream inputStream = contentResolver.openInputStream(imageUri);
            Bitmap urlToBitmap= BitmapFactory.decodeStream(inputStream);

//            String imagePath = getRealPathFromURI(imageUri);
           Bitmap addeffectBitmap=preprocessImage(urlToBitmap);

            // Initialize the Aspose.Words Document
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Insert the image into the document
            builder.insertImage(urlToBitmap);

            // Define the HTML file path
            File htmlFile = new File(getFilesDir(), "Output3.html");

            doc.save(htmlFile.getAbsolutePath());

            // Read the saved HTML file as a string
            String htmlContent = readHtmlFileAsString(htmlFile);
//
//            // Show the HTML content in an EditText
                Log.d("%%%", "saveImageAsHtml: "+htmlContent);
            binding.etText.setText(htmlContent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Error saving HTML: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

when i convert then show this types reult:-

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta name="generator" content="Aspose.Words for Android via Java 24.6.0" />
    <title></title>
</head><body style="font-family:'Times New Roman'; font-size:12pt">
    <div><p style="margin-top:0pt; margin-bottom:0pt"><img src="Output3.001.png" width="624" height="133" alt="" style="-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-wrap-type:inline; -aw-top-pos:0pt" /></p></div>
</body>

and when i try same image in “Convert Image To HTML Online” accurate result

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta name="generator" content="Aspose.Words for .NET 24.11.1" />
    <title></title>
    <style type="text/css">
        body {
            font-family: 'Times New Roman';
            font-size: 12pt
        }

        p {
            margin: 0pt
        }
    </style>
</head><body>
    <div><p style="margin-top:1.65pt; margin-right:6pt; text-indent:89.25pt; line-height:46.35pt"><span style="font-family:Calibri; font-size:33pt">Company</span><span style="font-family:Calibri; font-size:33pt; letter-spacing:139.15pt"> </span><span style="font-family:Calibri; font-size:33pt">Contact</span><span style="font-family:Calibri; font-size:33pt; letter-spacing:29.65pt"> </span><span style="font-family:Calibri; font-size:33pt">|</span><span style="font-family:Calibri; font-size:33pt; letter-spacing:9.45pt"> </span><span style="font-family:Calibri; font-size:33pt">Country Alfreds</span><span style="font-family:Calibri; font-size:33pt; letter-spacing:18.25pt"> </span><span style="font-family:Calibri; font-size:33pt">Futterkiste</span><span style="font-family:Calibri; font-size:33pt; letter-spacing:66.2pt"> </span><span style="font-family:Calibri; font-size:33pt">{Maria</span><span style="font-family:Calibri; font-size:33pt; letter-spacing:83.85pt"> </span><span style="font-family:Calibri; font-size:33pt">Germany</span></p><p style="margin-top:8.3pt; margin-left:1.45pt; text-align:justify; line-height:43.9pt"><span style="font-family:Calibri; font-size:36pt">Centro</span><span style="font-family:Calibri; font-size:36pt; letter-spacing:230.6pt"> </span><span style="font-family:Calibri; font-size:36pt">Francisco</span><span style="font-family:Calibri; font-size:36pt; letter-spacing:34.7pt"> </span><span style="font-family:Calibri; font-size:36pt">||Mexico</span></p></div>
</body>

so fix this issue in android java when convert image to html face some issue

private void saveImageAsHtml(Uri imageUri) {
        try {
            // Convert URI to a file path
            ContentResolver contentResolver = getContentResolver();

            // Open an InputStream using the URI
            InputStream inputStream = contentResolver.openInputStream(imageUri);
            Bitmap urlToBitmap= BitmapFactory.decodeStream(inputStream);

//            String imagePath = getRealPathFromURI(imageUri);
           Bitmap addeffectBitmap=preprocessImage(urlToBitmap);

            // Initialize the Aspose.Words Document
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Insert the image into the document
            builder.insertImage(urlToBitmap);

            // Define the HTML file path
            File htmlFile = new File(getFilesDir(), "Output3.html");

            doc.save(htmlFile.getAbsolutePath());

            // Read the saved HTML file as a string
            String htmlContent = readHtmlFileAsString(htmlFile);
//
//            // Show the HTML content in an EditText
                Log.d("%%%", "saveImageAsHtml: "+htmlContent);
            binding.etText.setText(htmlContent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Error saving HTML: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

when i convert then show this types reult:-

and when i try same image in “Convert Image To HTML Online” accurate result

so fix this issue in android java

@jayeshinfyom

To convert an image to HTML using Aspose.Words for Android via Java, you can follow the steps outlined below. It seems you are encountering issues with the output HTML not matching the expected format. Here’s a refined approach to ensure better results:

  1. Load the Image: Ensure that the image is correctly loaded from the URI.
  2. Insert the Image into a Document: Use the DocumentBuilder to insert the image into a new document.
  3. Save the Document as HTML: Use the appropriate save options to ensure the HTML output is formatted correctly.

Here’s an updated version of your code that incorporates these steps:

private void saveImageAsHtml(Uri imageUri) {
    try {
        // Convert URI to a file path
        ContentResolver contentResolver = getContentResolver();
        InputStream inputStream = contentResolver.openInputStream(imageUri);
        Bitmap urlToBitmap = BitmapFactory.decodeStream(inputStream);

        // Initialize the Aspose.Words Document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Insert the image into the document
        builder.insertImage(urlToBitmap);

        // Set up HTML save options for better formatting
        HtmlSaveOptions saveOptions = new HtmlSaveOptions();
        saveOptions.setPrettyFormat(true); // Makes the HTML output more readable

        // Define the HTML file path
        File htmlFile = new File(getFilesDir(), "Output3.html");

        // Save the document as HTML
        doc.save(htmlFile.getAbsolutePath(), saveOptions);

        // Read the saved HTML file as a string
        String htmlContent = readHtmlFileAsString(htmlFile);
        Log.d("HTML Output", "saveImageAsHtml: " + htmlContent);
        binding.etText.setText(htmlContent);
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "Error saving HTML: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

Key Changes:

  • HtmlSaveOptions: This allows you to customize the HTML output. Setting setPrettyFormat(true) will help in generating more readable HTML.
  • Error Handling: Ensure that you handle exceptions properly to catch any issues during the image loading or document saving process.

Additional Tips:

  • If the output still does not match what you see on the Aspose conversion tool, consider checking the image format and dimensions, as well as any specific styles or attributes that may be applied in the web version.
  • You may also want to explore the documentation for more advanced options related to rendering documents to HTML, which can provide insights into handling complex formatting.

For further details on rendering documents to HTML, you can refer to the official Aspose documentation here.

If you continue to face issues, please provide more details about the specific problems you are encountering with the output HTML.

ok but i mean not get accurate result like “Convert Image To HTML Online

@jayeshinfyom Could you please attach your input image here? Do you mean that image should be converted to text?

@jayeshinfyom Thank you for additional information. Aspose.Words itself does not support OCR. You can try using Aspose.OCR to convert image to text:
https://docs.aspose.com/ocr/java/