欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式

 更新時間:2015年11月18日 09:43:23   投稿:mrr  
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式的相關(guān)資料,需要的朋友可以參考下

本文將講解如何通過codecogs.com和Google.com提供的API接口來將LaTeX數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式。具體思路如下:

      (1)通過EditText獲取用戶輸入的LaTeX數(shù)學(xué)表達(dá)式,然后對表達(dá)式格式化使之便于網(wǎng)絡(luò)傳輸。

      (2)將格式化之后的字符串,通過Http請求發(fā)送至codecogs.com或者Google.com。

      (3)獲取網(wǎng)站返回的數(shù)據(jù)流,將其轉(zhuǎn)化為圖片,并顯示在ImageView上。

具體過程為:

1、獲取并格式化LaTeX數(shù)學(xué)表達(dá)式

      首先,我們在這個網(wǎng)站輸入LaTeX數(shù)學(xué)公式然后返回圖片時,即“http://latex.codecogs.com/gif.latex?“后面跟上我們輸入的公式內(nèi)容。比如”http://latex.codecogs.com/gif.latex?\alpha”就顯示一個希臘字母\alpha。所以我們可以在其后加上我們希望轉(zhuǎn)換的公式即可。但是需要注意的是,網(wǎng)絡(luò)URL中的空格有時候會自動轉(zhuǎn)化為加號”+“。所以,我們在傳輸?shù)臅r候需要將空格去掉?;蛘邔⑵滢D(zhuǎn)換為”%20“。Button單擊時執(zhí)行。

      首先要添加網(wǎng)絡(luò)訪問權(quán)限:

<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();         // 執(zhí)行Http請求
while(!finishFlag) {}              // 等待數(shù)據(jù)接收完畢
imageView.setImageBitmap(pngBM);        // 顯示圖片
finishFlag = false;               // 標(biāo)識回位

2、發(fā)送Http請求

      這里,我們發(fā)送Http請求采取異步線程的方式。首先,獲取上一步得到的URL地址,然后建立一個Http鏈接,然后將返回的數(shù)據(jù)輸入到輸入流中,最后將輸入流進(jìn)行解碼為圖片并顯示在ImageView中。

 protected Void doInBackground(Void... params) {
      try {
        URL picUrl = Url;              // 獲取URL地址
         HttpURLConnection conn = (HttpURLConnection) picUrl.openConnection();
//        conn.setConnectTimeout(1000);       // 建立連接
//        conn.setReadTimeout(1000);
        conn.connect();               // 打開連接
        if (conn.getResponseCode() == 200) {     // 連接成功,返回數(shù)據(jù)
          InputStream ins = conn.getInputStream(); // 將數(shù)據(jù)輸入到數(shù)據(jù)流中
          pngBM = BitmapFactory.decodeStream(picUrl.openStream()); // 解析數(shù)據(jù)流
          finishFlag = true;            // 數(shù)據(jù)傳輸完畢標(biāo)識
          ins.close();               // 關(guān)閉數(shù)據(jù)流
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }

完整的MyDownloadTask類代碼(在MainActivity內(nèi)):

3、顯示圖片

       在上一步建立的網(wǎng)絡(luò)連接類,然后在Button單擊事件內(nèi)實例化一個此類來接收數(shù)據(jù),然后將返回的數(shù)據(jù)顯示在ImageView內(nèi)。

btnPreview.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        try {
          Url = new URL(PicUrlCogs + editText.getText().toString().replace(" ","")); // 轉(zhuǎn)換字符串
          new MyDownloadTask().execute();         // 執(zhí)行Http請求
          while(!finishFlag) {}              // 等待數(shù)據(jù)接收完畢
          imageView.setImageBitmap(pngBM);        // 顯示圖片
          finishFlag = false;               // 標(biāo)識回位
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

        這樣,我們在輸入LaTeX公式之后,單擊PREVIEW按鈕,就會在ImageView上顯示對應(yīng)的圖片了。由于本文只討論如何進(jìn)行轉(zhuǎn)化,并沒有對圖片進(jìn)行任何優(yōu)化處理,可能看起來比較小。另外,如果采取去空格轉(zhuǎn)化URL的方法,盡量保證LaTeX表達(dá)式是嚴(yán)格合法的(比如所有單元都用{}括起來)。

Screenshot_2015-11-17-22-21-34 Screenshot_2015-11-17-22-23-00

完整代碼:

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&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(" ","")); // 轉(zhuǎn)換字符串
          new MyDownloadTask().execute();         // 執(zhí)行Http請求
          while(!finishFlag) {}              // 等待數(shù)據(jù)接收完畢
          imageView.setImageBitmap(pngBM);        // 顯示圖片
          finishFlag = false;               // 標(biāo)識回位
        } 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;              // 獲取URL地址
        HttpURLConnection conn = (HttpURLConnection) picUrl.openConnection();
//        conn.setConnectTimeout(1000);       // 建立連接
//        conn.setReadTimeout(1000);
        conn.connect();               // 打開連接
        if (conn.getResponseCode() == 200) {     // 連接成功,返回數(shù)據(jù)
          InputStream ins = conn.getInputStream(); // 將數(shù)據(jù)輸入到數(shù)據(jù)流中
          pngBM = BitmapFactory.decodeStream(picUrl.openStream()); // 解析數(shù)據(jù)流
          finishFlag = true;            // 數(shù)據(jù)傳輸完畢標(biāo)識
          ins.close();               // 關(guān)閉數(shù)據(jù)流
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }
    protected void onPostExecute(Void result) {
      // dismiss progress dialog and update ui
    }
  }
}

以上內(nèi)容是小編給大家介紹的關(guān)于Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式,希望大家喜歡。

相關(guān)文章

最新評論