-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
741 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
app/src/main/java/com/example/user/ncpaidemo/CpvActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.example.user.ncpaidemo; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.AsyncTask; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.Spinner; | ||
import android.widget.TextView; | ||
|
||
import com.ncp.ai.demo.process.CpvProc; | ||
|
||
public class CpvActivity extends BaseActivity { | ||
|
||
Button cpvBtn; | ||
|
||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_cpv); | ||
|
||
SharedPreferences sharedPref = getSharedPreferences("PREF", Context.MODE_PRIVATE); | ||
final String clientId = sharedPref.getString("application_client_id", ""); | ||
final String clientSecret = sharedPref.getString("application_client_secret", ""); | ||
|
||
cpvBtn = (Button) findViewById(R.id.btn_cpv); | ||
cpvBtn.setOnClickListener(new View.OnClickListener() { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
|
||
EditText cpvInputText = (EditText) findViewById(R.id.text_input_cpv); | ||
String strText = cpvInputText.getText().toString(); | ||
|
||
Spinner spinner = (Spinner)findViewById(R.id.spinner_cpv_lang); | ||
String selItem = spinner.getSelectedItem().toString(); | ||
|
||
String[] splits = selItem.split("\\("); | ||
|
||
String speaker = splits[0]; | ||
|
||
if (speaker.isEmpty() || speaker.equals("")){ | ||
speaker = "mijin"; | ||
} | ||
|
||
CpvActivity.NaverTTSTask tts = new CpvActivity.NaverTTSTask(); | ||
tts.execute(strText, speaker, clientId, clientSecret); | ||
} | ||
}); | ||
|
||
Spinner s = (Spinner)findViewById(R.id.spinner_cpv_lang); | ||
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||
@Override | ||
public void onItemSelected(AdapterView<?> parent, View view, | ||
int position, long id) { | ||
|
||
Spinner s = (Spinner)findViewById(R.id.spinner_cpv_lang); | ||
String lang = s.getSelectedItem().toString(); | ||
String text = ""; | ||
if (lang.contains("한국어")) { | ||
text = "네이버 클라우드 플랫폼에서는 Clova, papago 등 네이버의 다양한 인공지능 서비스를 API 형태로 제공합니다."; | ||
}else if (lang.contains("영어")) { | ||
text = "NAVER CLOUD PLATFORM provides various AI services in API formats, such as Clova and Papago."; | ||
}else if (lang.contains("일본어")) { | ||
text = "NAVER CLOUD PLATFORMは、ClovaやPapagoなどのAPIフォーマットでさまざまなAIサービスを提供します."; | ||
|
||
}else if (lang.contains("중국어")) { | ||
text = "NAVER CLOUD PLATFORM以API格式提供各种AI服务,例如Clova和Papago."; | ||
|
||
}else if (lang.contains("스페인어")) { | ||
text = "NAVER CLOUD PLATFORM proporciona varios servicios de AI en formatos API, como Clova y Papago."; | ||
|
||
}else { | ||
text = ""; | ||
} | ||
|
||
TextView textViewVersionInfo = (TextView) findViewById(R.id.text_input_cpv); | ||
textViewVersionInfo.setText(text); | ||
|
||
} | ||
@Override | ||
public void onNothingSelected(AdapterView<?> parent) {} | ||
}); | ||
|
||
} | ||
|
||
public class NaverTTSTask extends AsyncTask<String, String, String> { | ||
|
||
@Override | ||
public String doInBackground(String... strings) { | ||
|
||
CpvProc.main(strings[0], strings[1], strings[2], strings[3]); | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
app/src/main/java/com/example/user/ncpaidemo/OcrActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.example.user.ncpaidemo; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.os.AsyncTask; | ||
import android.os.Bundle; | ||
import android.provider.MediaStore; | ||
import android.provider.Settings; | ||
import android.support.design.widget.FloatingActionButton; | ||
import android.support.design.widget.Snackbar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.Toolbar; | ||
import android.util.JsonReader; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.Spinner; | ||
import android.widget.TextView; | ||
|
||
import com.ncp.ai.demo.process.NmtProc; | ||
import com.ncp.ai.demo.process.OcrProc; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
public class OcrActivity extends BaseActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_ocr); | ||
|
||
SharedPreferences sharedPref = getSharedPreferences("PREF", Context.MODE_PRIVATE); | ||
|
||
final String ocrApiGwUrl = sharedPref.getString("ocr_api_gw_url", ""); | ||
final String ocrSecretKey = sharedPref.getString("ocr_secret_key", ""); | ||
|
||
Button ocrTranslateBtn; | ||
|
||
ocrTranslateBtn = (Button) findViewById(R.id.btn_ocr_translate); | ||
ocrTranslateBtn.setOnClickListener(new View.OnClickListener() { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
|
||
OcrActivity.PapagoNmtTask nmtTask = new OcrActivity.PapagoNmtTask(); | ||
nmtTask.execute(ocrApiGwUrl, ocrSecretKey); | ||
} | ||
}); | ||
} | ||
|
||
public class PapagoNmtTask extends AsyncTask<String, String, String> { | ||
|
||
@Override | ||
public String doInBackground(String... strings) { | ||
|
||
return OcrProc.main(strings[0], strings[1]); | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(String result) { | ||
|
||
ReturnThreadResult(result); | ||
} | ||
} | ||
|
||
public void ReturnThreadResult(String result) { | ||
System.out.println("### Retrun Thread Result"); | ||
String translateText = ""; | ||
|
||
String rlt = result; | ||
try { | ||
JSONObject jsonObject = new JSONObject(rlt); | ||
|
||
JSONArray jsonArray = jsonObject.getJSONArray("images"); | ||
|
||
for (int i = 0; i < jsonArray.length(); i++ ){ | ||
|
||
JSONArray jsonArray_fields = jsonArray.getJSONObject(i).getJSONArray("fields"); | ||
|
||
for (int j=0; j < jsonArray_fields.length(); j++ ){ | ||
|
||
String inferText = jsonArray_fields.getJSONObject(j).getString("inferText"); | ||
translateText += inferText; | ||
translateText += " "; | ||
} | ||
} | ||
|
||
TextView txtResult = (TextView) findViewById(R.id.textView_ocr_result); | ||
txtResult.setText(translateText); | ||
|
||
} catch (Exception e){ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.ncp.ai.demo.process; | ||
|
||
import android.media.MediaPlayer; | ||
import android.os.Environment; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
|
||
public class CpvProc { | ||
|
||
|
||
public static void main(String msg, String speaker, String clientId, String clientSecret) { | ||
|
||
try { | ||
String text = URLEncoder.encode(msg, "UTF-8"); | ||
String apiURL = "https://naveropenapi.apigw.ntruss.com/voice-premium/v1/tts"; | ||
URL url = new URL(apiURL); | ||
HttpURLConnection con = (HttpURLConnection)url.openConnection(); | ||
con.setRequestMethod("POST"); | ||
con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId); | ||
con.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret); | ||
// post request | ||
String postParams = "speaker="+speaker+"&speed=0&text="+text; | ||
System.out.println(postParams); | ||
con.setDoOutput(true); | ||
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | ||
wr.writeBytes(postParams); | ||
wr.flush(); | ||
wr.close(); | ||
int responseCode = con.getResponseCode(); | ||
BufferedReader br; | ||
System.out.println("## response code : "+responseCode); | ||
if(responseCode==200) { // 정상 호출 | ||
InputStream is = con.getInputStream(); | ||
int read = 0; | ||
byte[] bytes = new byte[1024]; | ||
|
||
File dir = new File(Environment.getExternalStorageDirectory()+"/", "NCP"); | ||
|
||
if(!dir.exists()){ | ||
dir.mkdirs(); | ||
} | ||
|
||
String tempname = "csstemp"; | ||
File f = new File( Environment.getExternalStorageDirectory() + File.separator + "NCP/" + tempname + ".mp3"); | ||
f.createNewFile(); | ||
OutputStream outputStream = new FileOutputStream(f); | ||
while ((read =is.read(bytes)) != -1) { | ||
outputStream.write(bytes, 0, read); | ||
} | ||
is.close(); | ||
|
||
String pathToFile = Environment.getExternalStorageDirectory() + File.separator + "NCP/" + tempname + ".mp3"; | ||
MediaPlayer audioPlay = new MediaPlayer(); | ||
audioPlay.setDataSource(pathToFile); | ||
audioPlay.prepare(); | ||
audioPlay.start(); | ||
|
||
System.out.println("## fie path : "+ pathToFile); | ||
|
||
} else { // 에러 발생 | ||
br = new BufferedReader(new InputStreamReader(con.getErrorStream())); | ||
String inputLine; | ||
StringBuffer response = new StringBuffer(); | ||
while ((inputLine = br.readLine()) != null) { | ||
response.append(inputLine); | ||
} | ||
br.close(); | ||
System.out.println(response.toString()); | ||
} | ||
} catch (Exception e) { | ||
System.out.println(e); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.