日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java图像处理之图像裁剪

發(fā)布時間:2024/1/23 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java图像处理之图像裁剪 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? ? ? ??圖像裁剪即截取原始圖像某一部分生成一幅新的圖像,某些app也會要求用戶將上傳圖像進行一定程度裁剪來作為頭像。圖像裁剪實現(xiàn)起來比較簡單,下面介紹兩種裁剪方式,矩形裁剪和圓形裁剪。

? ? ? ??矩形裁剪,定義圖像上某個坐標(x,y)作為裁剪起始位置,xLength和yLength作為水平方向和垂直方向裁剪長度。由于裁剪范圍可能超限,即裁剪起始位置到裁剪長度超過原始圖像圖像范圍,需要判斷(x+xLength)是否大于圖像寬width。

? ? ? ??圓形裁剪,定義圖像上某個坐標(x,y)作為圓心,設(shè)定裁剪半徑radius。判斷橫坐標是否超限,以radius是否大于圓心x和x+radius是否大于圖像寬度作為條件,如果超限,再判斷是圓心左側(cè)還是右側(cè),最后以最小值對radius重新賦值。縱坐標以同樣方式判斷是否超限,并確定是否對radius重新賦值。

? ? ? ??完整代碼如下:

public class ImageCut {/*** 矩形裁剪,設(shè)定起始位置,裁剪寬度,裁剪長度* 裁剪范圍需小于等于圖像范圍* @param image* @param xCoordinate* @param yCoordinate* @param xLength* @param yLength* @return*/public BufferedImage imageCutByRectangle(BufferedImage image, int xCoordinate, int yCoordinate, int xLength,int yLength) {//判斷x、y方向是否超過圖像最大范圍if((xCoordinate + xLength) >= image.getWidth()) {xLength = image.getWidth() - xCoordinate;}if ((yCoordinate + yLength) >= image.getHeight()) {yLength = image.getHeight() - yCoordinate;}BufferedImage resultImage = new BufferedImage(xLength, yLength, image.getType());for (int x = 0; x < xLength; x++) {for (int y = 0; y < yLength; y++) {int rgb = image.getRGB(x + xCoordinate, y + yCoordinate);resultImage.setRGB(x, y, rgb);}}return resultImage;}/*** 圓形裁剪,定義圓心坐標,半徑* 裁剪半徑可以輸入任意大于零的正整數(shù)* @param image* @param xCoordinate* @param yCoordinate* @param radius* @return*/public BufferedImage imageCutByCircle(BufferedImage image, int xCoordinate, int yCoordinate, int radius) {//判斷圓心左右半徑是否超限if ((xCoordinate + radius) > image.getWidth() || radius > xCoordinate) {int a = image.getWidth() - 1 - xCoordinate;if (a > xCoordinate) {radius = xCoordinate;}else {radius = a;}}//判斷圓心上下半徑是否超限if ((yCoordinate + radius) > image.getHeight() || radius >yCoordinate) {int a = image.getHeight() - 1 - yCoordinate;if (a > yCoordinate) {radius = yCoordinate;}else {radius = a;}}int length = 2 * radius + 1;BufferedImage resultImage = new BufferedImage(length, length, image.getType());for (int i = 0; i < length; i++) {for (int j = 0; j < length; j++) {int x = i - radius;int y = j - radius;int distance = (int) Math.sqrt(x * x + y * y);if (distance <= radius) {int rgb = image.getRGB(x + xCoordinate, y + yCoordinate);resultImage.setRGB(i, j, rgb);}}}return resultImage;}public static void main(String[] args) throws Exception {File input = new File("C:/Users/admin/Desktop/1.jpg");File output = new File("C:/Users/admin/Desktop/3.jpg");BufferedImage image = ImageIO.read(input);BufferedImage result = new ImageCut().imageCutByCircle(image, 80, 80, 80);ImageIO.write(result, "jpg", output);} }

? ? ? ? 測試一下

? ? ? ? 原圖:

矩形裁剪:

圓形裁剪:

?

總結(jié)

以上是生活随笔為你收集整理的java图像处理之图像裁剪的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。