java代码扣图 java编写图案

怎么用java实现抠图功能?package com.thinkgem.jeesite.modules.file.utils;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageUtils {
/**
* 图片去白色的背景,并裁切
*
* @param image 图片
* @param range 范围 1-255 越大 容错越高 去掉的背景越多
* @return 图片
* @throws Exception 异常
*/
public static byte[] transferAlpha(Image image, InputStream in, int range) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ImageIcon imageIcon = new ImageIcon(image);
BufferedImage bufferedImage = new BufferedImage(imageIcon
.getIconWidth(), imageIcon.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
.getImageObserver());
int alpha = 0;
int minX = bufferedImage.getWidth();
int minY = bufferedImage.getHeight();
int maxX = 0;
int maxY = 0;
for (int j1 = bufferedImage.getMinY(); j1bufferedImage
.getHeight(); j1) {
for (int j2 = bufferedImage.getMinX(); j2bufferedImage
.getWidth(); j2) {
int rgb = bufferedImage.getRGB(j2, j1);
int R = (rgb0xff0000)16;
int G = (rgb0xff00)8;
int B = (rgb0xff);
if (((255 - R)range)((255 - G)range)((255 - B)range)) { //去除白色背景;
rgb = ((alpha1)24) | (rgb0x00ffffff);
} else {
minX = minX = j2 ? minX : j2;
minY = minY = j1 ? minY : j1;
maxX = maxX = j2 ? maxX : j2;
maxY = maxY = j1 ? maxY : j1;
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
int width = maxX - minX;
int height = maxY - minY;
BufferedImage sub = bufferedImage.getSubimage(minX, minY, width, height);
int degree = getDegree(in);
sub = rotateImage(sub,degree);
ImageIO.write(sub, "png", byteArrayOutputStream);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return byteArrayOutputStream.toByteArray();
}
/**
* 图片旋转
* @param bufferedimage bufferedimage
* @param degree 旋转的角度
* @return BufferedImage
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
w, h)), degree);
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(rect_des.width, rect_des.height, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.translate((rect_des.width - w) / 2,
(rect_des.height - h) / 2);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 计算旋转后图像的大小
* @param srcRectangle
* @param degree 旋转的角度
* @return Rectangle
*/
public static Rectangle CalcRotatedSize(Rectangle src, int degree) {
if (degree = 90) {
if(degree / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
degree = degree % 90;
}
double r = Math.sqrt(src.height * src.heightsrc.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(degree) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(degree)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.widthlen_dalta_width * 2;
int des_height = src.heightlen_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
/**
* byte[] ------BufferedImage
*
* @param byteImage byteImage
* @return return
* @throws IOException IOException
*/
public static BufferedImage ByteToBufferedImage(byte[] byteImage) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(byteImage);
return ImageIO.read(in);
}
/**
* 获取照片信息的旋转角度
* @param inputStream 照片的路径
* @return 角度
*/
public static int getDegree(InputStream inputStream) {
try {
Metadata metadata = https://www.04ip.com/post/ImageMetadataReader.readMetadata(new BufferedInputStream(inputStream),true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if ("Orientation".equals(tag.getTagName())) {
return turn(getOrientation(tag.getDescription()));
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
return 0;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
return 0;
}
/**
* 获取旋转的角度
* @param orientation orientation
* @return 旋转的角度
*/
public static int turn(int orientation) {
Integer turn = 360;
if (orientation == 0 || orientation == 1) {
turn = 360;
} else if (orientation == 3) {
turn = 180;
} else if (orientation == 6) {
turn = 90;
} else if (orientation == 8) {
turn = 270;
}
return turn;
}
/**
* 根据图片自带的旋转的信息 获取 orientation
* @param orientation orientation
* @return orientation
*/
public static int getOrientation(String orientation) {
int tag = 0;
if ("Top, left side (Horizontal / normal)".equalsIgnoreCase(orientation)) {
tag = 1;
} else if ("Top, right side (Mirror horizontal)".equalsIgnoreCase(orientation)) {
tag = 2;
} else if ("Bottom, right side (Rotate 180)".equalsIgnoreCase(orientation)) {
tag = 3;
} else if ("Bottom, left side (Mirror vertical)".equalsIgnoreCase(orientation)) {
tag = 4;
} else if ("Left side, top (Mirror horizontal and rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 5;
} else if ("Right side, top (Rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 6;
} else if ("Right side, bottom (Mirror horizontal and rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 7;
} else if ("Left side, bottom (Rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 8;
}
return tag;
}
}
我有一个签名图片如何用java代码裁剪掉空白区域用imageIO 读取进入BufferedImage,检测图片非空白大小 , 建立新的BufferedImage,拷贝非空白区至新的BufferedImage, 用imageIO 再次保存 。
java中如何用鼠标点击截取一张图片的某部分(希望有具体代码)用map标签java代码扣图 , 在drw里用热区java代码扣图,拖动就可以了java代码扣图,java代码扣图你试试
代码会自动生成 , 如下:
map
name="Map"
id="Map"area
shape="rect"
coords="104,303,223,357"
href=""
/
/map
你只要换掉104,303,223,357(图片区域上下左右坐标的位置)和超链接地址即可
如何用Java 实现抠图?选中你想抠图的图片,右键,选中编辑选项,进入"画图"程序
2、按住"Ctrl A",选中图片,"Ctrl C"复制图片
【java代码扣图 java编写图案】关于java代码扣图和java编写图案的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读