Jinshan internship weekly (4) -- Google cloud print

Source: Internet
Author: User

When javax. print calls sun. print. in win32printservice, we realized that this was already a platform issue. When we found that most of the shared printers were host-based printers (that is, they had no processing capability and only knew the dot mode, it is declared that javax is transplanted. print's plan went bankrupt. In this way, only the last solution is left-using a third-party library. After consideration, Google cloud print (GCP) was selected ).

Let's share with you how to learn about cloud printing.

Why cloud printing?

Cloud printing allows applications on any device (desktop, mobile phone, or mobile phone) to use any self-owned printer on the cloud without the need to install a print driver. This feature makes cloud printing very suitable for use on mobile phones.

Current cloud Printing

GCP, which was launched from January 25, is still in beta version. This is the only vendor that provides the cloud printing service. However, there are two conditions for use: one is to have a Google account; the other is to install chrome on the PC connected to the printer if it is not a cloud printer.
HP cloud print is actually a new cloud printer eprint series, which is easy to use-send emails directly to the printer.
Motoprint, a cloud-based printing application launched by Motorola on Android phones. No official version has been found.
Airprint, Apple's built-in printing service in IOS 4.2, can only be used on HP's eprint series. The official version has been released.
GCP is the only choice among the above four options.

PS: GCP is used in China without GFW conversion. a url exception may occur... The probability of success is about 30%.

Use GCP

To use GCP on the client, you only need to perform the following three steps: 1. obtain the authorization code, 2. Find available printers, and 3. Submit the code to the GCP server. Because the GCP official website does not provide the Java version example, so here is a Java version.

Get authorization code

You can exchange an authorization code by submitting your Google account and password to the server.

URL url = new URL ("https://www.google.com/accounts/ClientLoginaccountType=HOSTED_OR_GOOGLE&Email=" + m_sname + "& passwd =" + m_spw + "& service = cloudprint & source = Lin"); httpurlconnection connection = (httpurlconnection) URL. openconnection (); bufferedreader reader = new bufferedreader (New inputstreamreader (connection. getinputstream (), "US-ASCII"); string line; while (line = reader. readline ())! = NULL) {string [] split = line. split ("="); If (split. length = 2 & Split [0]. equals ("auth") {m_sauthcode = Split [1]; // The authorization code is here }}

Find available printers

Upload the obtained authorization code to the specified HTTP address.

URL url = new URL ("http://www.google.com/cloudprint/search? Output = JSON "); httpurlconnection connection = (httpurlconnection) URL. openconnection (); connection. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded"); connection. addrequestproperty ("Authorization", "googlelogin auth =" + m_sauthcode); bufferedreader reader = new bufferedreader (New inputstreamreader (connection. getinputstream (), "US-ASCII"); string line; while (line = reader. readline ())! = NULL) {saveprinters (line); // The printer information is stored in line. Write a function to save it. >_<}
 

Submit to GCP Server

The ID of the printer to be used, the type of the printed file, and the printed content and authorization code are post to the specified URL.

URL url = new URL ("http://www.google.com/cloudprint/submit? Output = JSON "); httpurlconnection connection = (httpurlconnection) URL. openconnection (); connection. setdooutput (true); connection. setrequestmethod ("Post"); connection. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded"); connection. addrequestproperty ("Authorization", "googlelogin auth =" + m_sauthcode); string posttext = "printerid =" + urlencoder. encode (printerid) + // printer ID obtained in the previous step "& capabilities =" + urlencoder. encode ("") + "& contenttype =" + urlencoder. encode ("application/pdf") + // print type "& Title =" + urlencoder. encode (title) + "& content =" + getstringfrompath (PATH); // convert the printed data to stringbyte [] postbytes = posttext. getbytes (); connection. getoutputstream (). write (postbytes); connection. getoutputstream (). flush (); connection. getoutputstream (). close ();
Now, the job is submitted. And my print task is over, yeah !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.