tadeuespindola

foto-nao-obrigatoria

May 17th, 2021
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. public String salvar() throws IOException {
  2.        
  3.         byte[] imagemByte = null;
  4.         if (arquivofoto != null) {
  5.             imagemByte = getByte(arquivofoto.getInputStream());            
  6.         }
  7.        
  8.         if (imagemByte != null && imagemByte.length > 0) {
  9.             /* processar a imagem */       
  10.             pessoa.setFotoIconBase64Original(imagemByte); //salva a foto original
  11.            
  12.             //transformar em bufferimage
  13.             BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imagemByte));
  14.            
  15.             //pega o tipo da imagem
  16.             int type = bufferedImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType();
  17.            
  18.             int largura = 200;
  19.             int altura = 200;
  20.            
  21.             //criar a miniatura da imagem
  22.             BufferedImage resizeImage = new BufferedImage(altura, largura, type);
  23.             Graphics2D g = resizeImage.createGraphics();
  24.             g.drawImage(bufferedImage, 0, 0, largura, altura, null);
  25.             g.dispose();
  26.            
  27.             //escrever novamente a imagem em tamanho menor
  28.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  29.             String extensao = arquivofoto.getContentType().split("\\/")[1]; //image/png -- a posicao do array é pq a extensao do arquivo vem depois de image
  30.             ImageIO.write(resizeImage, extensao, baos);
  31.            
  32.             String miniImagem = "data:" + arquivofoto.getContentType() + ";base64," + DatatypeConverter.printBase64Binary(baos.toByteArray());
  33.                        
  34.             /* processar a imagem */
  35.            
  36.             /* processar a imagem part final */
  37.             pessoa.setFotoIconBase64(miniImagem);
  38.             pessoa.setExtensao(extensao);
  39.             /* processar a imagem part final */
  40.         }      
  41.        
  42.         daoGeneric.salvar(pessoa);
  43.         pessoa = new Pessoa(); 
  44.         carregarPessoas();
  45.         mostrarMsg("cadastrado com sucesso!");
  46.         return "";
  47.     }
Add Comment
Please, Sign In to add comment