Android development Note (29) Http-based LaTeX mathematical formula converter, androidlatex

Source: Internet
Author: User

Android development Note (29) Http-based LaTeX mathematical formula converter, androidlatex

This article explains how to convert LaTeX mathematical function expressions to image formats through the API interfaces provided by codecogs.com and Google.com. The specific ideas are as follows:

(1) obtain the LaTeX mathematical expression entered by the user through EditText, and format the expression to Facilitate network transmission.

(2) Send the formatted string to codecogs.com or Google.com through an Http request.

(3) obtain the data stream returned by the website, convert it to an image, and display it on the ImageView.

The specific process is as follows:

1. Obtain and format LaTeX Mathematical Expressions

First, we enter the LaTeX mathematical formula on this website and then return the picture, that is, "http://latex.codecogs.com/gif.latex? "Follow the formula we entered. For example, "http://latex.codecogs.com/gif.latex? \ Alpha is a Greek letter. So we can add the formula we want to convert. However, the spaces in the web URL are sometimes automatically converted to the plus sign (+). Therefore, we need to remove spaces during transmission. Or convert it to "% 20". Button is executed when it is clicked.

First, you must add the network access permission:

 <uses-permission android:name="android.permission.INTERNET"/>
String PicUrlCogs = "http://latex.codecogs.com/gif.latex? "; Url = new URL (PicUrlCogs + editText. getText (). toString (). replace ("", ""); new mydownloadtask(cmd.exe cute (); // execute the Http request while (! FinishFlag) {}// wait until the data is received. imageView. setImageBitmap (pngBM); // display the image finishFlag = false; // identify the Back-to-bit

2. Send an Http request

Here, the Http request is sent in asynchronous thread mode. First, obtain the URL obtained in the previous step, create an Http link, input the returned data to the input stream, and then decode the input stream as an image and display it in ImageView.

Protected Void doInBackground (Void... params) {try {URL picUrl = Url; // obtain the URL address HttpURLConnection conn = (HttpURLConnection) picUrl. openConnection (); // conn. setConnectTimeout (1000); // establish a connection // conn. setreadtimeouts (1000); conn. connect (); // open the connection if (conn. getResponseCode () = 200) {// The connection is successful. InputStream ins = conn is returned. getInputStream (); // input data to the data stream pngBM = BitmapFactory. decodeStream (picUrl. openStream (); // parse the data stream finishFlag = true; // The data transmission end identifier (ins. close (); // close the data stream} catch (Exception e) {e. printStackTrace ();} return null ;}

Complete MyDownloadTask class code (within MainActivity ):

Class MyDownloadTask extends AsyncTask <Void, Void, Void> {protected void onPreExecute () {// display progress dialog .} protected Void doInBackground (Void... params) {try {URL picUrl = Url; // obtain the URL address HttpURLConnection conn = (HttpURLConnection) picUrl. openConnection (); // conn. setConnectTimeout (1000); // establish a connection // conn. setreadtimeouts (1000); conn. connect (); // open the connection if (conn. getResponseCode () = 200) {// The connection is successful. InputStream ins = conn is returned. getInputStream (); // input data to the data stream pngBM = BitmapFactory. decodeStream (picUrl. openStream (); // parse the data stream finishFlag = true; // The data transmission end identifier (ins. close (); // close the data stream} catch (Exception e) {e. printStackTrace ();} return null;} protected void onPostExecute (Void result) {// dismiss progress dialog and update ui }}

3. display images

In the network connection class established in the previous step, instantiate a class in the Button click event to receive data, and then display the returned data in the ImageView.

BtnPreview. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {try {Url = new URL (PicUrlCogs + editText. getText (). toString (). replace ("", ""); // convert the string new mydownloadtask(cmd.exe cute (); // execute the Http request while (! FinishFlag) {}// wait until the data is received. imageView. setImageBitmap (pngBM); // display image finishFlag = false; // identify return} catch (Exception e) {e. printStackTrace ();}}});

In this way, after entering the LaTeX formula, click the PREVIEW button to display the corresponding image on the ImageView. Since this article only discusses how to convert images without any optimization, it may seem relatively small. In addition, if the URL is converted by spaces, try to ensure that the LaTeX expression is strictly legal (for example, all units are included in ).

Import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. asyncTask; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. imageView; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; public class MainActivity extends Activity {private String PicUrlGoogle = "http://chart.apis.google.com/chart? Cht = tx & chlorophyll = "; private String PicUrlCogs =" http://latex.codecogs.com/gif.latex? "; Private Button btnPreview; private EditText editText; private ImageView imageView; private Bitmap pngBM; private URL Url; private boolean finishFlag = false; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnPreview = (Button) findViewById (R. id. btnPreview); imageView = (ImageView) findViewById (R. id. imageV Iew); editText = (EditText) findViewById (R. id. editText); btnPreview. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {try {Url = new URL (PicUrlCogs + editText. getText (). toString (). replace ("", ""); // convert the string new mydownloadtask(cmd.exe cute (); // execute the Http request while (! FinishFlag) {}// wait until the data is received. imageView. setImageBitmap (pngBM); // display image finishFlag = false; // identify return} catch (Exception e) {e. printStackTrace () ;}}) ;}@ Override public void onDestroy () {super. onDestroy ();} class MyDownloadTask extends AsyncTask <Void, Void, Void> {protected void onPreExecute () {// display progress dialog .} protected Void doInBackground (Void... params) {try {URL picUrl = Url; // obtain the URL address HttpURLConnection conn = (HttpURLConnection) picUrl. openConnection (); // conn. setConnectTimeout (1000); // establish a connection // conn. setreadtimeouts (1000); conn. connect (); // open the connection if (conn. getResponseCode () = 200) {// The connection is successful. InputStream ins = conn is returned. getInputStream (); // input data to the data stream pngBM = BitmapFactory. decodeStream (picUrl. openStream (); // parse the data stream finishFlag = true; // The data transmission end identifier (ins. close (); // close the data stream} catch (Exception e) {e. printStackTrace ();} return null;} protected void onPostExecute (Void result) {// dismiss progress dialog and update ui }}}

Related Article

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.