Search This Blog

Wednesday 27 August 2014

print the photos through android camera

Print images: 

In now days taking photos and sharing in social sites and chat application is normal in now days. all these thing we can done by mobile or tablets we don't need desktop or laptop. if your mobile device allow to user taking a photos and sharing them in social sites then it also allow to print the that photos.in all android device having a android support library which provide the function of the enabling the print the photos . these function use the minimal amount of code for print the images.

Here for print the images using the V4 support library and printHelper class.

How to print images:

private void doPhotoPrint() {
    PrintHelper photoPrinter = new PrintHelper(getActivity());
    photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.droids);
    photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}

as sow in above for print the images it uses a print helper class in simply way. it has a single layout option setScalmode(). these class provide the select the one of the two option:

1) SCALE_MODE_FIT:  This option provide the sizes the images so that whole images show with in a printable area of the page.

2) SCALE_MODE_FILL: This option scales so that fills the printable area of the page.here choose this option means your images will be crop. it means some part of the images like bottom area or some part of edges of images or top area will be not display.

These method can called as action for menu item but some times these menu item not supported so it will be put in to the over flow menu.

Here printHelper class called the printBitmap() method. after calling that method no further action required by your application . User have two option when it uses a print option 1) print images and 2) cancel the process. if user choose first then it job on the print the images and notification appear on to the system bar. 


Print the HTML document:

here this feature provide to you additional content in your printouts beyond just print the images.here android requires composing text and graphics in a print document. the android 4.4 provide the web view class for enable print the HTML document. The class allow to you load a local HTML document resource and download from the web.  

Load the HTML document:

Print the HTML document with the webview involves loading an HTML building or resources of HTML as a string.this portion describe how to load an HTML documents in webview  for printing.
private WebView mWebView;
private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, " +
            "testing, testing...</p></body></html>";
    webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);

    // Keep a reference to WebView object until you pass the PrintDocumentAdapter
    // to the PrintManager
    mWebView = webView;
}

Create a print job :

private void createWebPrintJob(WebView webView) {

    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getActivity()
            .getSystemService(Context.PRINT_SERVICE);

    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

    // Create a print job with name and adapter instance
    String jobName = getString(R.string.app_name) + " Document";
    PrintJob printJob = printManager.print(jobName, printAdapter,
            new PrintAttributes.Builder().build());

    // Save the job object for later status checking
    mPrintJobs.add(printJob);
}  
After loading an HTML content in to the webviwe then it's time for the create a job for print that document. Here printManager class uses for print the document.This example saves an instance of the projectjob object for use by the application  which is not required  . your application may uses this object of track the progress of  the print job as it's being .

This approach is useful when you want to promote the status of the print the document in your application for completion, failure or user cancellation.creating application notification is not required because the print framework automatically creates a system notification for the print job.

1 comment: