import java.io.*;
import javax.microedition.io.*;
import com.nttdocomo.io.*;
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.*;
import com.nttdocomo.system.*;
import com.nttdocomo.util.*;
import com.nttdocomo.net.*;
import com.nttdocomo.device.Camera;
public class comm_client extends IApplication {
TopPanel tp;
MyPanel mp;
MyCanvas mc;
ImageLabel w_imagelabel;
Camera w_camera;
Label label_result;
MediaImage lbl_animgif;
Image w_image;
String result;
int g_index = -1;
int g_height[];
int g_width[];
int g_x[];
int g_y[];
int g_leye_x[];
int g_leye_y[];
int g_reye_x[];
int g_reye_y[];
int target_color_num = 4;
int identify_num = 0;
int pressed_num = -1;
public void start(){
//オブジェクトを生成
tp = new TopPanel();
mp = new MyPanel();
mc = new MyCanvas();
tp.setSoftLabel(mp.SOFT_KEY_1,"終了");
tp.setSoftLabel(mp.SOFT_KEY_2,"開始");
Display.setCurrent(tp);
}
//*************************************************************************
class TopPanel extends Panel implements SoftKeyListener,KeyListener {
Image image;
ImageLabel imglab;
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
public TopPanel() {
setLayoutManager(null);
setSoftKeyListener(this);
setKeyListener(this);
MediaImage mi = MediaManager.getImage ("resource:///image/top.jpg");
try{
mi.use();
}catch (Exception e) {System.out.println("0" + e);}
image = mi.getImage();
imglab = new ImageLabel(image);
add(imglab);
int dwidth = Display.getWidth();
int dheight = Display.getHeight();
int iwidth = image.getWidth();
int iheight = image.getHeight();
imglab.setLocation(dwidth / 2 - iwidth / 2,dheight / 2 - iheight / 2);
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
public void softKeyReleased(int pparam){
if(pparam == this.SOFT_KEY_1){
IApplication.getCurrentApp().terminate();
} else if (pparam == this.SOFT_KEY_2) {
mp.setSoftLabel(mp.SOFT_KEY_1,"終了");
mp.setSoftLabel(mp.SOFT_KEY_2,"結果");
Display.setCurrent(mp);
}
}
public void softKeyPressed(int pparam){}
//------------------------------
//
//------------------------------
public void keyPressed(Panel tp,int key){
if (key == Display.KEY_SELECT){
mp.setSoftLabel(mp.SOFT_KEY_1,"終了");
mp.setSoftLabel(mp.SOFT_KEY_2,"結果");
Display.setCurrent(mp);
}
}
public void keyReleased(Panel tp,int type){}
}
//********************************************************
class MyPanel extends Panel implements SoftKeyListener,ComponentListener {
ImageEncode ec = new ImageEncode();
private VisualPresenter anim_gif;
Button button_photo;
Button button_gazou;
Button button_save;
ListBox list_identify;
Label label_setsumei_tcolor;
ListBox list_targetcolor;
//------------------------------
//
//------------------------------
public MyPanel(){
setLayoutManager(null);
int top_pos = 5;
int char_width = 12;
int dwidth = Display.getWidth();
int dheight = Display.getHeight();
w_camera = Camera.getCamera(0);
lbl_animgif = MediaManager.getImage("resource:///image/karetanuki.gif");
try{
lbl_animgif.use();
}catch(Exception e){}
anim_gif = new VisualPresenter();
anim_gif.setImage(lbl_animgif);
add(anim_gif);
int iwidth = lbl_animgif.getWidth();
int iheight = lbl_animgif.getHeight();
anim_gif.setLocation(dwidth / 2 - iwidth / 2 - 40,top_pos);
anim_gif.play();
//----------------------------------
label_result = new Label("お顔検出の結果を表示",1);
add(label_result);
label_result.setLocation(dwidth / 2 - iwidth / 2 + 20,top_pos + 30);
//----------------------------------
button_photo = new Button("写真撮影");
add(button_photo);
button_photo.requestFocus();
button_photo.setLocation(dwidth / 2 - char_width * 4 / 2,top_pos + 60);
//----------------------------------
button_gazou = new Button("画像選択");
//add(button_gazou);
//button_gazou.setLocation(dwidth / 2 - char_width * 4 / 2,top_pos + 90);
//----------------------------------
label_setsumei_tcolor = new Label("矩形の色を指定");
add(label_setsumei_tcolor);
label_setsumei_tcolor.setLocation(dwidth / 2 - char_width * 7 / 2,top_pos + 90);
list_targetcolor = new ListBox(ListBox.CHOICE);
list_targetcolor.append("赤");
list_targetcolor.append("青");
list_targetcolor.append("灰");
list_targetcolor.append("黒");
list_targetcolor.append("白");
list_targetcolor.append("緑");
list_targetcolor.append("黄");
add(list_targetcolor);
list_targetcolor.setLocation(dwidth / 2 + char_width * 7 / 2 + 5,top_pos + 90);
list_targetcolor.setSize(30,20);
//----------------------------------
list_identify = new ListBox(ListBox.CHOICE);
list_identify.append("矩形に識別番号をつけない");
list_identify.append("矩形に識別番号をつける");
add(list_identify);
list_identify.setLocation(dwidth / 2 - char_width * 12 / 2 + 5,top_pos + 120);
list_identify.setSize(char_width * 13,20);
//----------------------------------
button_save = new Button("結果保存");
add(button_save);
button_save.setLocation(dwidth / 2 - char_width * 4 / 2,top_pos + 180);
//----------------------------------
w_imagelabel = new ImageLabel();
//----------------------------------
setSoftKeyListener(this);
setComponentListener(this);
}
//------------------------------
//
//------------------------------
public void softKeyReleased(int param){
if(param == this.SOFT_KEY_1){
terminate();
}
if(param == this.SOFT_KEY_2){
show_image();
}
}
//------------------------------
//
//------------------------------
public void softKeyPressed(int param){}
//------------------------------
//
//------------------------------
public void componentAction(Component c,int type,int param){
if ((c == button_photo)&&(type == BUTTON_PRESSED)){
try {
w_camera.takePicture();
} catch (Exception ce) {}
MediaImage mo;
try{
mo = w_camera.getImage(0);
}catch(ArrayIndexOutOfBoundsException e){return;}
try {
mo.use();
} catch (Exception ce) {}
w_image = mo.getImage();
//
w_imagelabel.setImage(w_image);
w_imagelabel.setVisible(false);
anim_gif.play();
label_result.setText("画像処理中");
imageOut();
}
if ((c == list_targetcolor)&&(type == SELECTION_CHANGED)) {
boolean sel_tcolor_flag;
sel_tcolor_flag = false;
for (int i = 0;i < 7;i++) {
sel_tcolor_flag = list_targetcolor.isIndexSelected(i);
if (sel_tcolor_flag == true) {
if (i == 0) {//RED
target_color_num = 4;
}else if (i == 1) {//BLUE
target_color_num = 1;
}else if (i == 2) {//GRAY
target_color_num = 8;
}else if (i == 3) {//BLACK
target_color_num = 0;
}else if (i == 4) {//WHITE
target_color_num = 7;
}else if (i == 5) {//GREEN
target_color_num = 2;
}else if (i == 6) {//YELLOW
target_color_num = 6;
}
break;
}
}
}
if ((c == list_identify)&&(type == SELECTION_CHANGED)) {
boolean sel_identify_flag;
sel_identify_flag = false;
for (int i = 0;i < 2;i++) {
sel_identify_flag = list_identify.isIndexSelected(i);
if (sel_identify_flag == true) {
if (i == 0) {//つけない
identify_num = 0;
}else if (i == 1) {//つける
identify_num = 1;
}
break;
}
}
}
if ((c == button_save)&&(type == BUTTON_PRESSED)) {
mc.setSoftLabel(mc.SOFT_KEY_1,"終了");
mc.setSoftLabel(mc.SOFT_KEY_2,"戻る");
Display.setCurrent(mc);
ec.image_save();
}
}
//------------------------------
//
//------------------------------
public void imageOut() {
HttpConnection w_connect;
InputStream w_in;
OutputStream wo_stream;
byte[] w_data;
int w_length;
try {
w_in=w_camera.getInputStream(0);
String gname = Phone.getProperty(Phone.TERMINAL_ID);
String w_url = new String(getSourceURL() + "getImage.php?gname=" + gname);
w_connect = (HttpConnection)Connector.open(w_url,Connector.READ_WRITE,true);
w_connect.setRequestMethod(HttpConnection.POST);
w_connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
wo_stream = w_connect.openOutputStream();
w_data=new byte[128];
while ((w_length=w_in.read(w_data)) != -1 ) {
wo_stream.write(w_data,0,w_length);
}
w_in.close();
wo_stream.close();
w_connect.connect();
InputStream wi_stream = w_connect.openInputStream();
InputStreamReader wi_buffer = new InputStreamReader(wi_stream);
StringBuffer w_StringBuffer = new StringBuffer();
String w_remsg;
int w_char;
while ((w_char = wi_buffer.read()) != -1) {
w_StringBuffer.append((char)w_char);
}
w_remsg = (String)w_StringBuffer.toString();
result = w_remsg;
String ret = split_string(result,":","mae");
g_index = Integer.parseInt(ret);
if (g_index != 0) {
result = split_string(result,":","ato");
g_height = new int[g_index];
g_width = new int[g_index];
g_x = new int[g_index];
g_y = new int[g_index];
g_leye_x = new int[g_index];
g_leye_y = new int[g_index];
g_reye_x = new int[g_index];
g_reye_y = new int[g_index];
if (g_index > 10) {
g_index = 10;
}
String res = "";
int temp_index;
for (int i = 0;i < g_index;i++) {
temp_index = i * 8 + 1;
res = pickup_split(result,"/",temp_index + 0);
g_height[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 1);
g_width[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 2);
g_x[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 3);
g_y[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 4);
g_leye_x[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 5);
g_leye_y[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 6);
g_reye_x[i] = Integer.parseInt(res);
res = pickup_split(result,"/",temp_index + 7);
g_reye_y[i] = Integer.parseInt(res);
}
label_result.setText("検出完了");
}else{
label_result.setText("検出できませんでした");
}
//animationGIF
anim_gif.stop();
wi_stream.close();
w_connect.close();
//
//
}
catch (Exception e){label_result.setText("画像処理エラー");}
}
}
//------------------------------
//
//------------------------------
public void imageIn(){
HttpConnection w_http;
InputStream w_in;
OutputStream w_out;
byte[] w_data;
int w_length;
String filename = "";
try {
filename = "mmm.jpg";
w_http=(HttpConnection)(Connector.open("http://orchard.ddo.jp/iAPP/comm_server/" + filename,Connector.READ));
w_http.setRequestMethod(HttpConnection.GET);
w_http.connect();
w_in=w_http.openInputStream();
w_out=Connector.openOutputStream("scratchpad:///0;pos=0");
w_data=new byte[128];
while ((w_length=w_in.read(w_data)) != -1 ) {
w_out.write(w_data,0,w_length);
}
w_in.close();
w_out.close();
w_http.close();
System.out.println("ok");
//
} catch (IOException e){System.out.println("err:" + e);}
}
//------------------------------
//
//------------------------------
public String pickup_split(String target_string,String delimiter,int split_number){
int delimiter_pos = 0;
String temp_str = "";
String delimit_cont = "";
temp_str = target_string;
for (int i = 0;i < split_number;i++) {
delimiter_pos = temp_str.indexOf(delimiter);
if (delimiter_pos != -1) {
int str_len = temp_str.length();
delimit_cont = temp_str.substring(0,delimiter_pos);
//
temp_str = temp_str.substring(delimiter_pos + 1,str_len);
}
}
return delimit_cont;
}
//------------------------------
//
//------------------------------
public String split_string(String target_string,String delimiter,String split_pos){
String splited_str = "";
int str_len = target_string.length();
int delimiter_pos = target_string.indexOf(delimiter);
if (split_pos == "mae") {
splited_str = target_string.substring(0,delimiter_pos);
} else if (split_pos == "ato") {
splited_str = target_string.substring(delimiter_pos + 1,str_len);
}
return splited_str;
}
//------------------------------
//
//------------------------------
public void show_image(){
if (w_image == null) {
return;
}
if (g_index == 0) {
return;
}
mc.setSoftLabel(mc.SOFT_KEY_1,"終了");
mc.setSoftLabel(mc.SOFT_KEY_2,"戻る");
Display.setCurrent(mc);
}
//********************************************************
class ImageEncode{
Image image;
MediaImage mImage;
ImageStore store;
ImageEncode(){
image = null;
mImage=null;
}
//------------------------------
//
//------------------------------
void image_save(){
int a;
ImageEncoder ie = ImageEncoder.getEncoder("JPEG");
EncodedImage ei = ie.encode(mc,0,0,mc.getWidth(),mc.getHeight());
InputStream in = ei.getInputStream();
try{
OutputStream out = Connector.openOutputStream("scratchpad:///0;pos=0");
a = in.read();
out.write((byte)a);
while(a >= 0){
a = in.read();
out.write((byte)a);
}
in.close();
out.flush();
out.close();
}catch(IOException e){return;}
mImage = MediaManager.getImage("scratchpad:///0;pos=0");
try{
mImage.use();
}catch(ConnectionException ce){return;}
image=mImage.getImage();
try{
store = ImageStore.selectEntry();
store.addEntry(mImage);
}catch(InterruptedOperationException e2){return;}
}
}
//********************************************************
class MyCanvas extends Canvas{
ImageEncode ec = new ImageEncode();
//------------------------------
//
//------------------------------
public MyCanvas() {
super();
}
//------------------------------
//
//------------------------------
public void paint(Graphics g){
if (w_image != null){
g.lock();
g.clearRect(0,0,500,500);
g.drawImage(w_image,0,0);
g.setColor(g.getColorOfName(target_color_num));
if (g_index != 0) {
if (pressed_num == -1) {
for (int i = 0;i < g_index;i++) {
g.drawRect(g_x[i],g_y[i],g_width[i],g_height[i]);
if (identify_num == 1) {
g.drawString(String.valueOf(i),g_x[i],g_y[i] - 5);
}
}
}else{
for (int i = 0;i < g_index;i++) {
if (i == pressed_num) {
g.drawRect(g_x[i],g_y[i],g_width[i],g_height[i]);
if (identify_num == 1) {
g.drawString(String.valueOf(i),g_x[i],g_y[i] - 5);
}
}
}
}
}
g.unlock(true);
}
}
//------------------------------
//
//------------------------------
public void processEvent(int type, int param){
if (type == Display.KEY_RELEASED_EVENT) {
if (param == Display.KEY_SOFT1) {
IApplication.getCurrentApp().terminate();
} else if (param == Display.KEY_SOFT2) {
mp.setSoftLabel(mp.SOFT_KEY_1,"終了");
mp.setSoftLabel(mp.SOFT_KEY_2,"結果");
Display.setCurrent(mp);
} else if (param == Display.KEY_ASTERISK) {
pressed_num = -1;
} else if (param == Display.KEY_POUND) {
pressed_num = 1000;//最大数のdummy
} else if (param == 1) {
pressed_num = 1;
} else if (param == 2) {
pressed_num = 2;
} else if (param == 3) {
pressed_num = 3;
} else if (param == 4) {
pressed_num = 4;
} else if (param == 5) {
pressed_num = 5;
} else if (param == 6) {
pressed_num = 6;
} else if (param == 7) {
pressed_num = 7;
} else if (param == 8) {
pressed_num = 8;
} else if (param == 9) {
pressed_num = 9;
} else if (param == 0) {
pressed_num = 0;
}
}
repaint();
}
}
}