代码之家  ›  专栏  ›  技术社区  ›  laraib

在服务器上上载加密图像

  •  -1
  • laraib  · 技术社区  · 7 年前

    我在用这个 https://www.youtube.com/watch?v=EmOzhbGFEAk&t=1118s

    这是我的主要活动课

    public class MainActivity extends AppCompatActivity {
    
    EditText editName, editfatherName, editCNIC, editReview, editRegistration;
    Button btn_add, btn_retrieve;
    ImageView takePhoto, openGallery, UploadImage, ivImage;
    CameraPhoto cameraPhoto;
    GalleryPhoto galleryPhoto;
    final int CAMERA_REQUEST = 13323;
    final int GALLERY_REQUEST=22131;
    String SelectedPhoto;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraPhoto=new CameraPhoto(getApplicationContext());
        galleryPhoto=new GalleryPhoto(getApplicationContext());
        takePhoto=(ImageView)findViewById(R.id.Takephoto);
        openGallery=(ImageView)findViewById(R.id.OpenGallery);
        UploadImage=(ImageView)findViewById(R.id.Image_upload);
        ivImage=(ImageView)findViewById(R.id.image);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                try {
                    startActivityForResult(cameraPhoto.takePhotoIntent(),CAMERA_REQUEST);
                } catch (IOException e) {
                    Toast.makeText(MainActivity.this, "Something wrong while taking photos", Toast.LENGTH_SHORT).show();
                }
    
            }
        });
        openGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                startActivityForResult(galleryPhoto.openGalleryIntent(),GALLERY_REQUEST);
    
    
            }
        });
        UploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                try {
                    Bitmap bitmap= ImageLoader.init().from(SelectedPhoto).requestSize(1024,1024).getBitmap();
    
    //Change
    //Store encrypt string
    String encryptString=BitMapToString(bitmap);
    //whenever you want to use image
    Bitmap image=StringToBitmap(encryptString);
    //
    
                    String encodedImage = ImageBase64.encode(bitmap);
                    Log.d("UploadImaes",encodedImage);
                    HashMap<String,String> postData = new HashMap<String, String>();
                    postData.put("image",encodedImage); // here the size of Image is zero
    
                    PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
                        @Override
                        public void processFinish(String s) {
                             if(s.contains("uploaded_success"))
                            {
                                Toast.makeText(MainActivity.this, "Image Upload Successfully", Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                Toast.makeText(MainActivity.this, "Image Upload Failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    task.execute("http://localhost/news/upload.php");
                    task.setEachExceptionsHandler(new EachExceptionsHandler() {
                        @Override
                        public void handleIOException(IOException e) {
                            Toast.makeText(MainActivity.this, "Cannot Connect to Server 1", Toast.LENGTH_SHORT).show();
                        }
    
                        @Override
                        public void handleMalformedURLException(MalformedURLException e) {
                            Toast.makeText(MainActivity.this, "Cannot Connect to Server 2", Toast.LENGTH_SHORT).show();
    
                        }
    
                        @Override
                        public void handleProtocolException(ProtocolException e) {
                            Toast.makeText(MainActivity.this, "Cannot Connect to Server 3", Toast.LENGTH_SHORT).show();
    
                        }
    
                        @Override
                        public void handleUnsupportedEncodingException(UnsupportedEncodingException e) {
                            Toast.makeText(MainActivity.this, "Cannot Connect to Server 4", Toast.LENGTH_SHORT).show();
    
                        }
                    });
    
                } catch (FileNotFoundException e) {
                    Toast.makeText(MainActivity.this, "Something wrong while encoding photos", Toast.LENGTH_SHORT).show();
    
                }
    
            }
    
        }); 
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if(resultCode==RESULT_OK)
          {
              if(requestCode==CAMERA_REQUEST)
              {
                 String photoPath =cameraPhoto.getPhotoPath();
                SelectedPhoto=photoPath;
                  try {
                      Bitmap bitmap= ImageLoader.init().from(photoPath).requestSize(1024,1024).getBitmap();
                      ivImage.setImageBitmap(bitmap);
                  } catch (FileNotFoundException e) {
                      Toast.makeText(MainActivity.this, "Something wrong while loading photos", Toast.LENGTH_SHORT).show();
                  }
              }
              else  if(requestCode==GALLERY_REQUEST)
              {
                  Uri uri=data.getData();
                  galleryPhoto.setPhotoUri(uri);
                  String photoPath=galleryPhoto.getPath();
                  SelectedPhoto=photoPath;
                  try {
                      Bitmap bitmap= ImageLoader.init().from(photoPath).requestSize(1024,1024).getBitmap();
                      ivImage.setImageBitmap(bitmap);
                  } catch (FileNotFoundException e) {
                      Toast.makeText(MainActivity.this, "Something wrong while choosing photos", Toast.LENGTH_SHORT).show();
                  }
    
              }
          }
    }
    
    //change
    Copy and paste two function here.
    //
    

    这是我的Php脚本

     <?PHP
       if(isset($_POST['image']))
       {
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $id = $now->format('YmdHisu');
    $upload_folder = “upload"; 
    $path = "$upload_folder/$id.jpeg";
    $image = $_POST['image'];
    if(file_put_contents($path, base64_decode($image)) !=false)
    {
    echo “uploaded_success";
        exit;}
    else
    {
        echo “uploaded_failed";
    }
    }
        else {
    echo "image not in";
    exit;
       }  
       ?>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Vijay Chaudhary    7 年前

    为此,将图像转换为 位图,然后转换为Base64字符串

    public String BitMapToString(Bitmap bitmap) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] b = baos.toByteArray();
            String temp = Base64.encodeToString(b, Base64.NO_WRAP);
            return temp;
        }
    

    将位图转换为 使用以下函数

    public Bitmap StringToBitmap(String encodedstring) {
            Bitmap decodedByte;
            try {
                byte[] decodedString = Base64.decode(encodedstring, Base64.DEFAULT);
                decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    
            } catch (Exception e) {
                decodedByte = null;
            }
            return decodedByte;
        }