Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:background="@drawable/background"
  10. tools:context="com.example.ali.cottondiseasedetection.TakeImage">
  11. <TextView android:text="Please Choose an image From the Gallery or Capture a Photo."
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_marginTop="5dp"
  15. android:textColor="#ff0d5122"
  16. android:layout_centerHorizontal="true"
  17. android:textSize="20dp"
  18. android:textStyle="bold"
  19. />
  20. <LinearLayout
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_alignParentLeft="true"
  24. android:layout_marginTop="60dp"
  25. android:orientation="vertical" >
  26. <ImageButton
  27. android:id="@+id/button1"
  28. android:layout_width="140dp"
  29. android:layout_height="120dp"
  30. android:text="From Gallery"
  31. android:background="@drawable/imagegallery"
  32. />
  33. </LinearLayout>
  34. <LinearLayout
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_marginTop="45dp"
  38. android:layout_alignParentRight="true"
  39. android:orientation="vertical" >
  40. <ImageButton
  41. android:id="@+id/button"
  42. android:layout_width="140dp"
  43. android:layout_height="140dp"
  44. android:text="From Camera"
  45. android:layout_centerHorizontal="false"
  46. android:background="@drawable/vintagecamera"
  47. />
  48. </LinearLayout>
  49. <TableLayout
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_centerInParent="true"
  53.  
  54. >
  55. <TableRow android:layout_marginTop="150dp">
  56. <ImageView
  57. android:id="@id/setImage"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. />
  61. </TableRow>
  62. </TableLayout>
  63. <LinearLayout
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:layout_alignParentBottom="true">
  67. <Button
  68. android:id="@+id/process"
  69. android:layout_width="fill_parent"
  70. android:layout_height="fill_parent"
  71. android:text="Process Image"
  72. />
  73. </LinearLayout>
  74.  
  75. package com.example.ali.cottondiseasedetection;
  76.  
  77. public class TakeImage extends ActionBarActivity {
  78. String message2;
  79. private ProgressDialog pd;
  80. Context context;
  81. byte[] image;
  82. private Bitmap bitmap;
  83. private ImageButton camera;
  84. private ImageButton gallery;
  85. ImageView targetImage;
  86. NotificationManager nm;
  87. Uri fileUri = null;
  88. private Button process;
  89. private static int TAKE_PICTURE = 1;
  90. private static int FROM_GALLERY = 2;
  91. @Override
  92. protected void onCreate(Bundle savedInstanceState) {
  93. super.onCreate(savedInstanceState);
  94. setContentView(R.layout.activity_take_image);
  95. context=this;
  96. //Notification Code
  97. nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  98. //
  99. camera=(ImageButton)findViewById(R.id.button);
  100. gallery=(ImageButton)findViewById(R.id.button1);
  101. targetImage=(ImageView)findViewById(R.id.setImage);
  102. process=(Button)findViewById(R.id.process);
  103. camera.setOnClickListener(new View.OnClickListener() {
  104. @Override
  105. public void onClick(View v) {
  106. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
  107. File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
  108. intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
  109. fileUri = Uri.fromFile(photo);
  110. startActivityForResult(intent, TAKE_PICTURE);
  111. }
  112. });
  113. gallery.setOnClickListener(new View.OnClickListener() {
  114. @Override
  115. public void onClick(View v) {
  116. Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  117. startActivityForResult(intent, FROM_GALLERY);
  118. }
  119. });
  120. process.setOnClickListener(new View.OnClickListener() {
  121. @Override
  122. public void onClick(View v) {
  123. boolean temp=true;
  124. bitmap = BitmapFactory.decodeResource(getResources(), R.id.setImage);
  125. BitmapDrawable bd = (BitmapDrawable) targetImage.getDrawable();
  126. bitmap = bd.getBitmap();
  127. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  128. bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
  129. image = stream.toByteArray();
  130. int l = image.length;
  131. if (l == 0) {
  132. //Toast.makeText(getApplicationContext(),"0",Toast.LENGTH_LONG).show();
  133. } else {
  134.  
  135. }
  136. SendMessage sendMessageTask = new SendMessage();
  137. String re = null;
  138. try {
  139. re = sendMessageTask.execute().get();
  140. } catch (InterruptedException e) {
  141. e.printStackTrace();
  142. } catch (ExecutionException e) {
  143. e.printStackTrace();
  144. }
  145. //Toast.makeText(getApplicationContext(),"Processing",Toast.LENGTH_LONG).show();
  146.  
  147. Intent i = new Intent(getApplicationContext(), Response.class);
  148. i.putExtra("result", re);
  149. i.putExtra("image", image);
  150. startActivity(i);
  151.  
  152. }
  153. });
  154. }
  155. public void onConfigurationChanged(Configuration newConfig) {
  156. super.onConfigurationChanged(newConfig);
  157. }
  158. @Override
  159. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  160. // TODO Auto-generated method stub
  161. super.onActivityResult(requestCode, resultCode, data);
  162. if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
  163. Uri selectedImage = fileUri;
  164. getContentResolver().notifyChange(selectedImage, null);
  165. ContentResolver cr = getContentResolver();
  166. try {
  167. bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
  168. targetImage.setImageBitmap(bitmap);
  169. //Toast.makeText(this, selectedImage.toString(),Toast.LENGTH_LONG).show();
  170. } catch (Exception e) {
  171. //Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
  172. //.show();
  173. Log.e("Camera", e.toString());
  174. }
  175. }
  176. else if (requestCode == FROM_GALLERY && resultCode == RESULT_OK) {
  177. Uri targetUri = data.getData();
  178. try {
  179. bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
  180. targetImage.setImageBitmap(bitmap);
  181. } catch (FileNotFoundException e) {
  182. // TODO Auto-generated catch block
  183. e.printStackTrace();
  184. }
  185. }
  186. }
  187. private class SendMessage extends AsyncTask<String, String, String> {
  188. @Override
  189. protected void onPreExecute(){
  190. super.onPreExecute();
  191. pd = new ProgressDialog(TakeImage.this);
  192. pd.setMessage("Please wait.");
  193. pd.setIndeterminate(false);
  194. pd.setCancelable(true);
  195. pd.show();
  196. }
  197. protected String doInBackground(String... params) {
  198. Socket clientSocket = null;
  199. try {
  200. clientSocket = new Socket("192.168.1.8", 4001);
  201. } catch (IOException e) {
  202. e.printStackTrace();
  203. }
  204. assert clientSocket != null;
  205. DataOutputStream outToServer = null;
  206. try {
  207. outToServer = new DataOutputStream(clientSocket.getOutputStream());
  208. } catch (IOException e) {
  209. e.printStackTrace();
  210. }
  211.  
  212. // sentence = "Mohammad";
  213. assert outToServer != null;
  214. try {
  215. outToServer.writeInt(image.length);
  216. outToServer.write(image,0,image.length);
  217. } catch (IOException e) {
  218. e.printStackTrace();
  219. }
  220. DataInputStream input=null;
  221. try {
  222. input=new DataInputStream(clientSocket.getInputStream());
  223. } catch (IOException e) {
  224. e.printStackTrace();
  225. }
  226. assert input != null;
  227. try {
  228. message2=input.readUTF();
  229. } catch (IOException e) {
  230. e.printStackTrace();
  231. }
  232. try {
  233. clientSocket.close();
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. return message2;
  238. }
  239. @Override
  240. protected void onPostExecute(String s) {
  241. pd.dismiss();
  242. }
  243. }
  244.  
  245. @Override
  246. public boolean onCreateOptionsMenu(Menu menu) {
  247. // Inflate the menu; this adds items to the action bar if it is present.
  248. getMenuInflater().inflate(R.menu.menu_take_image, menu);
  249. return true;
  250. }
  251.  
  252. @Override
  253. public boolean onOptionsItemSelected(MenuItem item) {
  254. // Handle action bar item clicks here. The action bar will
  255. // automatically handle clicks on the Home/Up button, so long
  256. // as you specify a parent activity in AndroidManifest.xml.
  257. int id = item.getItemId();
  258. //noinspection SimplifiableIfStatement
  259. if (id == R.id.action_settings) {
  260. return true;
  261. }
  262. return super.onOptionsItemSelected(item);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement