This article will explain how to convert a latex mathematical function expression into a picture form using the API interface provided by Codecogs.com and Google.com. The specific ideas are as follows:
(1) Obtain the latex mathematical expression of the user input through EditText, and then format the expression to facilitate network transmission.
(2) Send the formatted string to the codecogs.com or google.com via HTTP request.
(3) Get the data stream returned by the website, convert it into a picture, and display it on the ImageView.
The specific process is:
1. Get and format latex mathematical expressions
First, we enter the latex mathematical formula on this site and then return to the picture, that is, "Http://latex.codecogs.com/gif.latex?" Follow the contents of the formula we entered. For example, "Http://latex.codecogs.com/gif.latex?\alpha" shows a Greek letter \alpha. So we can then add the formula we want to convert. However, it should be noted that the space in the network URL is sometimes automatically converted to the plus sign "+". Therefore, we need to remove the space when the transmission. or convert it to "%20". button to perform when clicked.
To add Network access permissions first:
<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 (). Execute (); Execute HTTP request
while (!finishflag) {} //Wait for data to be received
Imageview.setimagebitmap (PNGBM); Show Picture
Finishflag = false; Identify back-up
2. Send HTTP request
Here, we send HTTP requests in a way that takes an asynchronous thread. First, get the URL address from the previous step, then create an HTTP link, then enter the returned data into the input stream, and finally decode the input stream into a picture and display it in the ImageView.
protected void Doinbackground (void ... params) {
try {
url picurl = URL; Gets the URL address
httpurlconnection conn = (httpurlconnection) picurl.openconnection (); conn.setconnecttimeout (1000); Establish the connection
// conn.setreadtimeout (1000);
Conn.connect (); Open Connection
if (conn.getresponsecode () = = () { //connection succeeded, return data
inputstream ins = Conn.getinputstream ();///Data loss into the data stream
PNGBM = Bitmapfactory.decodestream (Picurl.openstream ());//Parse data stream
Finishflag = true; Data transmission Complete Identification
ins.close (); Close Data Flow
}
\ catch (Exception e) {
e.printstacktrace ();
}
return null;
}
Complete Mydownloadtask Class code (within mainactivity):
3. Display Picture
The network connection class that was established in the previous step, and then instantiate a class in the button click event to receive the data, and then display the returned data in ImageView.
Btnpreview.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
try {
url = new URL (picurlcogs + edittext.gettext (). toString (). Replace ("", "));//Convert String
new Mydownloadtask (). Execute (); Execute HTTP request
while (!finishflag) {} //Wait for data to be received
Imageview.setimagebitmap (PNGBM); Show Picture
Finishflag = false; Identifies the back-up
catch (Exception e) {
e.printstacktrace ();}}}
);
So, after we enter the Latex formula, click on the Preview button, and the corresponding picture will be displayed on the ImageView. Since this article discusses only how to convert, and does not optimize the image, it may look small. In addition, if you take the method of converting a URL to a space, try to ensure that the latex expression is strictly legal (for example, all cells are enclosed in {}).
Screenshot_2015-11-17-22-21-34 screenshot_2015-11-17-22-23-00
Complete code:
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&am
P;chl= ";
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.imageview); 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 string new Mydownloadt Ask (). Execute (); Execute HTTP request while (!finishflag) {}//wait for data to be received Imageview.setimagebitmap (PNGBM); Show Picture Finishflag = false;
Identifies the back-up catch (Exception e) {e.printstacktrace ();
}
}
});
@Override public void OnDestroy () {Super.ondestroy (); Class Mydownloadtask extends Asynctask<void, void, void> {protected Void OnPreExecute () {//display p
Rogress dialog. } protected void Doinbackground (void ... params) {try {url picurl = URL;
Gets the URL address httpurlconnection conn = (httpurlconnection) picurl.openconnection (); //conn.setconnecttimeout (1000);
Establish the connection//conn.setreadtimeout (1000); Conn.connect (); Open Connection if (conn.getresponsecode () = = 200) {//connection succeeded, return data InputStream ins = Conn.getinputstream (); /input data into the data stream PNGBM = Bitmapfactory.decodestream (Picurl.openstream ()); Parse data stream finishflag = true; Data transmission complete identification ins.close ();
Close Data Flow} catch (Exception e) {e.printstacktrace ();
return null; } protected void OnPostExecute (void result) {//Dismiss progress dialog and update UI}}}
The above content is small set to introduce about Android development learning notes through API interface will latex mathematical function expression into picture form, hope you like.