Este artigo é uma classe de processamento de imagem Java encapsulada pelo autor em combinação com algumas informações na Internet, suportando a escala, a rotação e a mosaicização das imagens.
Sem mais delongas, o código:
OFEM DE PACONE; import java.awt.color; importar java.awt.Graphics; importar java.awt.graphics2d; importar java.awt.image; importar java.awt.geom.fiFineTransform; import java.awt.Image.AffiTransformOp; import.FineTansform; javax.imageio.imageio;/*** Classe de processamento de imagem. * * @author nagsh * */public class IMAGEEAL {String OpenUrl; // a imagem original da imagem aberta string saveurl; // A nova imagem Salvar Path String Sorngame; // o novo nome da imagem string sufixo; // O novo tipo de imagem suporta apenas GIF, JPG, PNG Public IMAGEEAL (String OpenUrl, String saveurl, String SaveName, String sufix) {this.openurl = OpenUrl; this.SaveName = SaveName; this.saveurl = saveurl; this.Suffix = sufixo; } /*** Escala de imagem. * * @Param Width * Largura necessária * @Param Hight * Altura necessária * @throws Exception */ public void Zoom (Int Largura, Int Hight) lança Exceção {Double Sx = 0.0; duplo sy = 0,0; Arquivo de arquivo = novo arquivo (openUrl); if (! file.isfile ()) {lança uma nova exceção ("ImagemEal >>>" + arquivo + "não é um arquivo de imagem!"); } BufferImage bi = imageio.read (arquivo); // Leia a imagem // Calcule a taxa de escala do eixo x-eixo Y é necessária, certifique-se de que a largura e a altura dos parâmetros sejam alterações em escala igual antes de chamar = (duplo) largura/ bi.getWidth (); sy = (duplo) altura / bi.getHeight (); AFFINETRANSFORMOP OP = novo AffineTransformOp (AffineTransform.getScaleInstance (SX, SY), NULL); Arquivo sf = novo arquivo (saveurl, saveName + "." + Sufixo); Imagem zoomImage = op.filter (bi, nulo); tente {imageio.write ((bufferImage) zoomimage, sufixo, sf); // Salvar a imagem} Catch (Exceção e) {E.PrintStackTrace (); }} / ** * girar * * @param grau * girar ângulo * @throws Exceção * / public void spin (int grau) lança exceção {int swidth = 0; // largura após rotação int sheight = 0; // altura após rotação int x; // Origem coordenada horizontal int y; // Origem Arquivo de coordenadas verticais de origem = novo arquivo (OpenUrl); if (! file.isfile ()) {lança uma nova exceção ("ImagemEal >>>" + arquivo + "não é um arquivo de imagem!"); } BufferImage bi = imageio.read (arquivo); // Leia a imagem // Processar o grau de rotação do ângulo = grau % 360; if (grau <0) grau = 360 + grau; // converte o ângulo para entre 0-360 graus duplo teta = math.toradians (grau); // converte o ângulo em radianos // determinar a largura e a altura após a rotação se (grau == 180 || grau == 0 | sheight = bi.getHeight (); } else if (graus == 90 || grau == 270) {sheight = bi.getwidth (); swidth = bi.getHeight (); } else {swidth = (int) (math.sqrt (bi.getwidth () * bi.getwidth () + bi.getHeight () * bi.getHeight ())); sheight = (int) (math.sqrt (bi.getwidth () * bi.getwidth () + bi.getHeight ())); } x = (swidth / 2) - (bi.getwidth () / 2); // determinar a coordenada de origem y = (shight / 2) - (bi.getHeight () / 2); BufferImage spinimage = new bufferImage (swidth, sheight, bi.gettype ()); // Defina a cor de fundo da imagem Graphics2d GS = (Graphics2d) spinImage.getGraphics (); gs.setColor (color.white); GS.FillRect (0, 0, Swidth, Sheight); // desenha o fundo da imagem rotacionada em uma determinada cor afineTransform em = new AffineTransform (); at.rotate (teta, swidth / 2, sheight / 2); // girar a imagem at.Translate (x, y); AFFINETRANSFORMOP OP = new AffineTransformOp (at, AffineTransformOp.type_bicubic); spinimage = op.filter (bi, spinimage); Arquivo sf = novo arquivo (saveurl, saveName + "." + Sufixo); Imageio.write (spinimage, sufixo, sf); // Salvar a imagem} /*** Mosaicização. * Tamanho @param tamanho Mosaic Tamanho, ou seja, o comprimento e a largura de cada retângulo * @return * @throws Exception */ public boolean mosaic (size int) lança exceção {arquivo file = new File (OpenUrl); if (! file.isfile ()) {lança uma nova exceção ("ImagemEal >>>" + arquivo + "não é um arquivo de imagem!"); } BufferImage bi = imageio.read (arquivo); // Leia a imagem bufferImage spinImage = new bufferiMage (bi.getwidth (), bi.getHeight (), bi.type_int_rgb); if (bi.getWidth () <size || bi.getHeight () <size || tamanho <= 0) {// O tamanho da grade em mosaico é muito grande ou muito pequeno retorna false; } int xCount = 0; // O número de direção é desenhado int ycount = 0; // O número de direção é desenhado no YCount if (bi.getwidth () % size == 0) {xCount = bi.getWidth () / size; } else {xCount = bi.getWidth () / size + 1; } if (bi.getHeight () % size == 0) {ycount = bi.getHeight () / size; } else {ycount = bi.getHeight () / size + 1; } int x = 0; // coordena int y = 0; // Desenhe mosaicos (desenhe retângulos e preencha as cores) GS = spinImage.getGraphics (); for (int i = 0; i <xcount; i ++) {for (int j = 0; j <ycount; j ++) {// tamanho do retângulo em mosaico int mwidth = size; int mHeight = size; if (i == xcount-1) {// o último na direção horizontal é especial e pode não ser suficiente para um tamanho mwidth = bi.getwidth ()-x; } if (j == ycount-1) {// da mesma forma mHeight = bi.getHeight ()-y; } // O valor RGB da cor do retângulo leva o pixel central int Centerx = x; int Centery = y; if (mWidth % 2 == 0) {Centerx += mWidth / 2; } else {Centerx += (mWidth - 1) / 2; } if (mHeight % 2 == 0) {Centery += MHeight / 2; } else {Centery += (MHeight - 1) / 2; } Cor cor = nova cor (bi.getRgb (CenterX, Centery)); gs.setColor (cor); gs.fillrect (x, y, mwidth, mHeight); y = y + tamanho; // calcule as coordenadas y do próximo retângulo} y = 0; // restaurar as coordenadas y x = x + tamanho; // calcular as coordenadas x} gs.dispose (); Arquivo sf = novo arquivo (saveurl, saveName + "." + Sufixo); Imageio.write (spinimage, sufixo, sf); // salve a imagem retorna true; } public static void main (string [] args) lança a exceção {imageAgeAl IMAGEEAL = new IMAGEEAL ("e: //1.jpg", "e: //", "2", "jpg"); // Scaling de teste/* imageAghEal.Zoom (200, 300); */ // rotação de teste/* imageal.spin (90); */ // teste mosaico/*imageAghEal.mosaic (4);*/}} O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.