Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.03 KB | None | 0 0
  1. public class Register extends AppCompatActivity{
  2.  
  3. private final String TAG =this.getClass().getName();
  4.  
  5. Button btnreg;
  6. Button btncancel;
  7. EditText edUser,edPassword,edPassword2,edNick,edEmail,edEdad,edDescripcion;
  8. ImageView ivCamera,ivGallery,ivUpload,ivImage;
  9. Spinner spinnerDistrito,spinnerInteres;
  10. CameraPhoto cameraPhoto;
  11. GalleryPhoto galleryPhoto;
  12.  
  13. final int CAMERA_REQUEST=13323;
  14. final int GALLERY_REQUEST=22131;
  15.  
  16. String selectedPhoto="";
  17.  
  18. String idCliente;
  19. String msg;
  20.  
  21. DatabaseReference mDatabase= FirebaseDatabase.getInstance().getReference();
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_register2);
  27.  
  28. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  29.  
  30. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
  31. if(checkSelfPermission(android.Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
  32. ActivityCompat.requestPermissions(this,new String[] {android.Manifest.permission.CAMERA},1);
  33. }
  34. }
  35.  
  36. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
  37. if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
  38. ActivityCompat.requestPermissions(this,new String[] {android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
  39. }
  40. }
  41.  
  42. cameraPhoto=new CameraPhoto(getApplicationContext());
  43. galleryPhoto=new GalleryPhoto(getApplicationContext());
  44.  
  45. //ivCamera=(ImageView)findViewById(R.id.ivCamera);
  46. ivGallery=(ImageView)findViewById(R.id.ivGallery);
  47. //ivUpload=(ImageView)findViewById(R.id.ivGallery);
  48. ivImage=(ImageView)findViewById(R.id.ivimage);
  49. btncancel=(Button)findViewById(R.id.btnCancelar2);
  50. btnreg=(Button)findViewById(R.id.btnRegister2);
  51. edUser=(EditText)findViewById(R.id.et_user2);
  52. edPassword=(EditText)findViewById(R.id.et_password);
  53. edPassword2=(EditText)findViewById(R.id.et_password2);
  54. edNick=(EditText)findViewById(R.id.et_nick2);
  55. edEmail=(EditText)findViewById(R.id.et_email2);
  56. edEdad=(EditText)findViewById(R.id.et_edad2);
  57. edDescripcion=(EditText)findViewById(R.id.et_descripcion);
  58.  
  59. spinnerInteres=(Spinner) findViewById(R.id.spinner_Interes);
  60. spinnerDistrito=(Spinner)findViewById(R.id.spinnerDistrito2);
  61.  
  62. String[] distritos = {"Ancón","Ate","Barranco","Breña","Carabayllo","Chaclacayo", "Chorrillos", "Cieneguilla"
  63. , "Comas", "El Agustino", "Independencia (Lima)", "Jesús María", "La Molina", "La Victoria", "Lima", "Lince",
  64. "Los Olivos", "Lurigancho-Chosica", "Lurín", "Magdalena del Mar", "Miraflores", "Pachacámac",
  65. "Pucusana", "Pueblo Libre", "Puente Piedra", "Punta Hermosa", "Punta Negra", "Rímac", "San Bartolo", "San Borja",
  66. "San Isidro", "San Juan de Lurigancho", "San Juan de Miraflores", "San Luis", "San Martín de Porres", "San Miguel",
  67. "Santa Anita", "Santa María del Mar", "Santa Rosa (Lima)", "Santiago de Surco", "Villa El Salvador", "Villa María del Triunfo"};
  68. spinnerDistrito.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, distritos));
  69.  
  70. String[] intereses = {"Diversión","Diversión y apoyo económico","Relación abierta"};
  71. spinnerInteres.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,intereses));
  72. /*ivCamera.setOnClickListener(new View.OnClickListener() {
  73. @Override
  74. public void onClick(View v) {
  75. try {
  76. startActivityForResult(cameraPhoto.takePhotoIntent(),CAMERA_REQUEST);
  77. cameraPhoto.addToGallery();
  78. } catch (IOException e) {
  79. Toast.makeText(getApplicationContext(),"Something Wrong while taking photos",Toast.LENGTH_SHORT).show();
  80. }
  81. }
  82. });*/
  83.  
  84. ivGallery.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87. startActivityForResult(galleryPhoto.openGalleryIntent(),GALLERY_REQUEST);
  88. }
  89. });
  90.  
  91.  
  92. btnreg.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View v) {
  95. try {
  96. String edad=edEdad.getText().toString();
  97. final String user=edUser.getText().toString();
  98. String pass=edPassword.getText().toString();
  99. String pass2=edPassword2.getText().toString();
  100. String nick=edNick.getText().toString();
  101. final String email=edEmail.getText().toString();
  102. String descripcion=edDescripcion.getText().toString();
  103. String interes=spinnerInteres.getSelectedItem().toString();
  104. String distrito=spinnerDistrito.getSelectedItem().toString();
  105.  
  106. if(!user.equals("") && !pass.equals("") && !pass2.equals("") && !nick.equals("") && !email.equals("") && !edad.equals("") && !descripcion.equals("") && !interes.equals("")){
  107. if(pass.equals(pass2)){
  108. if(!selectedPhoto.equals("") || selectedPhoto!=null){
  109. Bitmap bitmap=ImageLoader.init().from(selectedPhoto).getBitmap();
  110.  
  111. String encodedImage= ImageBase64.encode(bitmap);
  112. //Log.d(TAG,encodedImage);
  113.  
  114. HashMap<String,String> postData=new HashMap<String, String>();
  115.  
  116. postData.put("image",encodedImage);
  117. postData.put("user",user);
  118. postData.put("password",pass);
  119. postData.put("nick",nick);
  120. postData.put("email",email);
  121. postData.put("edad",edad);
  122. postData.put("distrito",distrito);
  123. postData.put("descripcion",descripcion);
  124. postData.put("interes",interes);
  125.  
  126. PostResponseAsyncTask task=new PostResponseAsyncTask(Register.this, postData, new AsyncResponse() {
  127.  
  128. @Override
  129. public void processFinish(String s) {
  130. Log.d("JSON",s);
  131. try {
  132. JSONObject jsonObject=new JSONObject(s);
  133. idCliente = jsonObject.getString("id");
  134. msg = jsonObject.getString("msg");
  135. } catch (JSONException e) {
  136. msg = e.toString();
  137. }
  138. Log.d("msg",msg);
  139. if(msg.equals("uploaded_success")){
  140. mDatabase.child("Clientes").child("C"+idCliente).setValue(new User(user,email));
  141. Toast.makeText(getApplicationContext(),"Member registrado exitosamente",Toast.LENGTH_LONG).show();
  142. Intent intent=new Intent(getApplicationContext(),Login.class);
  143. startActivity(intent);
  144. }else if(msg.equals("user_existe")){
  145. Toast.makeText(getApplicationContext(),"El Usuario ya existe",Toast.LENGTH_LONG).show();
  146. }else if(msg.equals("nick_existe")){
  147. Toast.makeText(getApplicationContext(),"El Nick ya existe",Toast.LENGTH_LONG).show();
  148. }else if(msg.equals("uploaded_failed")){
  149. Toast.makeText(getApplicationContext(),"Cannot Connect to Server.",Toast.LENGTH_SHORT).show();
  150. }
  151. }
  152. });
  153. task.execute("http://blackmate.esy.es/blackmateapp/insertarCliente.php");
  154. task.setEachExceptionsHandler(new EachExceptionsHandler() {
  155. @Override
  156. public void handleIOException(IOException e) {
  157. Toast.makeText(getApplicationContext(),"Cannot Connect to Server.",Toast.LENGTH_SHORT).show();
  158. }
  159.  
  160. @Override
  161. public void handleMalformedURLException(MalformedURLException e) {
  162. Toast.makeText(getApplicationContext(),"URL Error.",Toast.LENGTH_SHORT).show();
  163. }
  164.  
  165. @Override
  166. public void handleProtocolException(ProtocolException e) {
  167. Toast.makeText(getApplicationContext(),"Protocol Error.",Toast.LENGTH_SHORT).show();
  168. }
  169.  
  170. @Override
  171. public void handleUnsupportedEncodingException(UnsupportedEncodingException e) {
  172. Toast.makeText(getApplicationContext(),"Encoding Error.",Toast.LENGTH_SHORT).show();
  173. }
  174. });
  175. }else{
  176. Toast.makeText(getApplicationContext(),"No haz seleccionado imagen",Toast.LENGTH_LONG).show();
  177. }
  178. }else{
  179. Toast.makeText(getApplicationContext(),"Las Contraseñas no coinciden",Toast.LENGTH_LONG).show();
  180. }
  181. }else{
  182. Toast.makeText(getApplicationContext(),"Faltan datos por completar",Toast.LENGTH_LONG).show();
  183. }
  184. } catch (FileNotFoundException e) {
  185. Toast.makeText(getApplicationContext(),"No haz seleccionado imagen",Toast.LENGTH_SHORT).show();
  186. }
  187. }
  188. });
  189.  
  190.  
  191. btncancel.setOnClickListener(new View.OnClickListener() {
  192. @Override
  193. public void onClick(View view) {
  194. Intent i=new Intent(getApplicationContext(),Login.class);
  195. startActivity(i);
  196. }
  197. });
  198.  
  199.  
  200.  
  201. }
  202.  
  203. public Bitmap redimensionarImagenMaximo(Bitmap mBitmap, float newWidth, float newHeigth){
  204. //Redimensionamos
  205. int width = mBitmap.getWidth();
  206. int height = mBitmap.getHeight();
  207. float scaleWidth = ((float) newWidth) / width;
  208. float scaleHeight = ((float) newHeigth) / height;
  209. // create a matrix for the manipulation
  210. Matrix matrix = new Matrix();
  211. // resize the bit map
  212. matrix.postScale(scaleWidth, scaleHeight);
  213. // recreate the new Bitmap
  214. return Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, false);
  215. }
  216.  
  217. @Override
  218. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  219. if(resultCode==RESULT_OK){
  220. if(requestCode==CAMERA_REQUEST){
  221. String photoPath= cameraPhoto.getPhotoPath();
  222. selectedPhoto=photoPath;
  223. Bitmap bitmap;
  224. try {
  225. bitmap= ImageLoader.init().from(photoPath).requestSize(512,512).getBitmap();
  226.  
  227. File imageFile = new File(photoPath);
  228. ExifInterface exif = null;
  229. try {
  230. exif = new ExifInterface(imageFile.getAbsolutePath());
  231. } catch (IOException e) {
  232. e.printStackTrace();
  233. }
  234. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  235. int rotate = 360;
  236. switch (orientation) {
  237. case ExifInterface.ORIENTATION_ROTATE_270:
  238. rotate = 270;
  239. break;
  240. case ExifInterface.ORIENTATION_ROTATE_180:
  241. rotate = 180;
  242. break;
  243. case ExifInterface.ORIENTATION_ROTATE_90:
  244. rotate = 90;
  245. break;
  246. }
  247.  
  248.  
  249. Matrix matrix = new Matrix();
  250. matrix.postRotate(rotate);
  251. Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,512,512,true);
  252. Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
  253.  
  254. ivImage.setImageBitmap(rotatedBitmap);
  255. } catch (FileNotFoundException e) {
  256. Toast.makeText(getApplicationContext(),"Something Wrong while loading photos",Toast.LENGTH_SHORT).show();
  257. }
  258. Log.d(TAG,photoPath);
  259. }else if(requestCode==GALLERY_REQUEST){
  260. Uri uri=data.getData();
  261. galleryPhoto.setPhotoUri(uri);
  262. String photoPath=galleryPhoto.getPath();
  263. selectedPhoto=photoPath;
  264. Bitmap bitmap;
  265. try {
  266. bitmap= ImageLoader.init().from(photoPath).requestSize(512,512).getBitmap();
  267.  
  268. File imageFile = new File(photoPath);
  269. ExifInterface exif = null;
  270. try {
  271. exif = new ExifInterface(imageFile.getAbsolutePath());
  272. } catch (IOException e) {
  273. e.printStackTrace();
  274. }
  275. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  276. int rotate = 360;
  277. switch (orientation) {
  278. case ExifInterface.ORIENTATION_ROTATE_270:
  279. rotate = 270;
  280. break;
  281. case ExifInterface.ORIENTATION_ROTATE_180:
  282. rotate = 180;
  283. break;
  284. case ExifInterface.ORIENTATION_ROTATE_90:
  285. rotate = 90;
  286. break;
  287. }
  288.  
  289.  
  290. Matrix matrix = new Matrix();
  291. matrix.postRotate(rotate);
  292. Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,512,512,true);
  293. Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
  294.  
  295.  
  296. ivImage.setImageBitmap(rotatedBitmap);
  297. } catch (FileNotFoundException e) {
  298. Toast.makeText(getApplicationContext(),"Something Wrong while choosing photos",Toast.LENGTH_SHORT).show();
  299. }
  300. }
  301. }
  302. }
  303. }
  304.  
  305. <?php
  306.  
  307. require("conexion.php");
  308.  
  309. $image =$_REQUEST['image'];
  310. $user = $_REQUEST['user'];
  311. $password = $_REQUEST['password'];
  312. $nick = $_REQUEST['nick'];
  313. $email = $_REQUEST['email'];
  314. $edad = $_REQUEST['edad'];
  315. $distrito =$_REQUEST['distrito'];
  316. $descripcion =$_REQUEST['descripcion'];
  317. $interes =$_REQUEST['interes'];
  318.  
  319. $now=DateTime::createFromFormat('U.u',microtime(true));
  320. $id=$now->format('YmdHisu');
  321.  
  322. $foto1 = "$id.jpg";
  323.  
  324. $mensaje="registro_insertado";
  325.  
  326. //verificando si existe usuario
  327. $consultaUser = "SELECT * FROM cliente WHERE User='$user'";
  328. $result = mysql_query($consultaUser);
  329. while ($fila = mysql_fetch_array($result)) {
  330. if($user == $fila['User']){
  331. $mensaje="user_existe";
  332. }
  333. }
  334.  
  335. //verificando si existe nick
  336. $consultaNick = "SELECT * FROM cliente WHERE Nick='$nick'";
  337. $result2 = mysql_query($consultaNick);
  338. while ($fila2 = mysql_fetch_array($result2)) {
  339. if($nick == $fila2['Nick']){
  340. $mensaje="nick_existe";
  341. }
  342. }
  343.  
  344.  
  345. if($mensaje=="registro_insertado"){
  346.  
  347. if($user!="" && $nick!=""){
  348.  
  349. $consulta = "INSERT INTO cliente (User,Password,Nick,Email,Distritos,Edad,Foto1,Descripcion,Intereses) VALUES ('$user','$password','$nick','$email','$distrito','$edad','$foto1','$descripcion','$interes')";
  350.  
  351. mysql_query($consulta);
  352.  
  353. $path="../imagenesCliente/$id.jpg";
  354.  
  355. /*if(file_put_contents($path,base64_decode($image)) != false){
  356.  
  357. $id = mysql_insert_id();
  358. $msg = "uploaded_success";
  359. $arr = array('msg' => $msg , 'id' => $id);
  360. echo json_encode($arr);
  361.  
  362. }else{
  363.  
  364. $msg = "uploaded_failed";
  365. $arr = array('msg' => $msg , 'id' => "");
  366. echo json_encode($arr);
  367.  
  368. }*/
  369.  
  370. $data = base64_decode($image); // base64 decoded image data
  371.  
  372. $source_img = imagecreatefromstring($data);
  373.  
  374. $rotated_img = imagerotate($source_img, 270, 0); // rotate with angle 90 here
  375.  
  376. $imageSave = imagejpeg($rotated_img, $path, 75);
  377.  
  378. imagedestroy($source_img);
  379.  
  380. $id = mysql_insert_id();
  381.  
  382. $msg = "uploaded_success";
  383.  
  384. $arr = array('msg' => $msg , 'id' => $id);
  385.  
  386. echo json_encode($arr);
  387.  
  388.  
  389.  
  390. }else{
  391. $msg = "uploaded_failed";
  392.  
  393. $arr = array('msg' => $msg , 'id' => "");
  394.  
  395. echo json_encode($arr);
  396. }
  397.  
  398. }else{
  399. $arr = array('msg' => $mensaje , 'id' => "");
  400. echo json_encode($arr);
  401. }
  402.  
  403. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement