0

Is it possible to check Base64 images before send to web server.Web server accept base64 image so i convert my image by this method:

   public void makeJSON_For_Image(int imageSize, String img_Address, String guid) {
        imageSizes = imageSize;
        img_Address = img_Address.substring(7, img_Address.length());
        byte[] bytes = null;
        byte[] buffer = new byte[8192];
        int bytesRead;
        try {
            InputStream input = new FileInputStream(img_Address);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            bytes = output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            LoggerExeption.log(e.getMessage() + "\n", e.getStackTrace());
        }
        String pic = Base64.encodeToString(bytes, Base64.DEFAULT);
}

But some times, service got an error because it can not convert base64 to an image.

Is it possible to check base64 that is correct converted or not?

Edit

According Alexandru Sandu suggestion I used this regular expression like this:

String pic = Base64.encodeToString(bytes, Base64.DEFAULT);
String regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
if(pic.matches(regex)){
    System.out.println("it's a Base64");
}

but my if condition return false !!! This is a part of my base46:

pic =

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYF
BgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoK
CgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAUAA8ADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwBtte/Z
4xAIcKowMCrEeoWz5WRRSG2i2+YhyMUi2UEwPOK/Pz7Qe1pbTjfE4AqNtOZcmKTPtTfslzFnyJMj
0zTVlukYiRelAEcsc8R5FIJkkXbIuCKtJcRyriUUjWccmSjjHoaB30K4Kj5Q2aQLNC28DINPk09l
+ZentUZaaLjOQKAWpKl0MESIDSiS3kHyAZqHzkIww/So2MYOUPWgLIsMkEnDAGmfZYlOFJH1pitF
nIkwfenRsRkM+R7GgQjWsmDsbI7iql1pVpKC0tsoP95OKviUJyvWkyJAS2D65oAx/wCyZRn7FeYI
/heml9YsgfPtyQP4k5Fa/kwsCE4I7ikEbrwGzQUmjNg1i3lylwOffrUyfZZM7WxUk9pZXGVuLdT7

Seems is it not correct!!

But i checkd my pic in this online base 64 checker, my image appeared completely...

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • What are the requirements for a valid image? If all images have to be .png, then you can verify the file signature (http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html) which is an inexpensive way of determining if an image is somewhat valid. – Zun Jul 03 '18 at 11:51
  • That image comes from mobile cammera and, yes it has png extention.Image is correct but base64 is Doubtful – Cyrus the Great Jul 03 '18 at 11:56
  • You want to make a check in code? or you just want to check it for the sake of development? if it is the latter get the base64 with a breakpoint and check it here: https://www.base64decode.org/ – Alexandru Sandu Jul 03 '18 at 12:13
  • If it is the first, check this thread: https://stackoverflow.com/questions/8571501/how-to-check-whether-the-string-is-base64-encoded-or-not – Alexandru Sandu Jul 03 '18 at 12:15

0 Answers0