我編寫(xiě)了一個(gè)函數(shù),其目的是獲取從重新縮放的輸入圖像定義的子圖像的集合(這些子圖像稱為“重新縮放的輸入圖像的重新縮放的塊”)并將這些重新縮放的塊重新組裝成輸出整個(gè)圖像(其尺寸是輸入圖像的那些,重新縮放)。所有塊都具有相同的尺寸。該函數(shù)定義如下。問(wèn)題是 SonarLint 警告我認(rèn)知復(fù)雜度是 16 而不是 15,而且我不想更改這個(gè)默認(rèn)的 SonarLint 限制。我能做什么?該功能的描述如下,代碼如下:final BufferedImage reassembleChunksInASingleImage(final int inputImageWidth, final int inputImageHeight, final int scalingCoefficient, final int chunkWidth, final int chunkHeight, final List<BufferedImage> theChunks) { logger.log(Level.INFO, "Reassembling..."); final int reassembled_chunks_image_width = scalingCoefficient * inputImageWidth; final int reassembledChunksImageHeight = scalingCoefficient * inputImageHeight; final int rescaled_chunk_width = scalingCoefficient * chunkWidth; final int rescaledChunkHeight = scalingCoefficient * chunkHeight; final BufferedImage reassembledChunksImage = new BufferedImage(reassembled_chunks_image_width, reassembledChunksImageHeight, BufferedImage.TYPE_INT_RGB); int indexOfTheChunkToUse = 0; for(int i = 0; i < reassembled_chunks_image_width; i += rescaled_chunk_width) { for(int j = 0; j < reassembledChunksImageHeight; j += rescaledChunkHeight) { final BufferedImage chunkToUse = theChunks.get(indexOfTheChunkToUse); int iForDraw = i; int jForDraw = j; final int deltaI = reassembled_chunks_image_width - (i + rescaled_chunk_width); final int deltaJ = reassembledChunksImageHeight - (j + rescaledChunkHeight); if(deltaI < 0) { iForDraw -= Math.abs(deltaI); }我瀏覽重新縮放的圖像,用塊的足夠(x 軸或 y 軸)尺寸(所有塊具有相同的尺寸)增加水平或垂直移位。相關(guān)循環(huán)是:for(int i = 0; i < reassembled_chunks_image_width; i += rescaled_chunk_width) {for(int j = 0; j < reassembledChunksImageHeight; j += rescaledChunkHeight) {我將當(dāng)前重新縮放圖像的像素顏色設(shè)置為當(dāng)前塊的顏色之一。相關(guān)循環(huán)是:for(int x = 0; x < rescaled_chunk_width; x++) {for(int y = 0; y < rescaledChunkHeight; y++) {.如果當(dāng)前塊超出了重新縮放圖像的范圍,我會(huì)將其移動(dòng)到左側(cè)或頂部,然后繪制它。相關(guān)控件是:if(deltaI < 0) {if(deltaJ < 0) {.
1 回答

Cats萌萌
TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
你可以嘗試重構(gòu)你的方法,例如
int iForDraw = getDraw(reassembled_chunks_image_width, rescaled_chunk_width, i);
int jForDraw = getDraw(reassembledChunksImageHeight, rescaledChunkHeight, j);
添加一個(gè)小方法,例如 getDraw
private int getDraw(int reassembled_chunks_image_data, int rescaled_chunk_data, int index) {
int result = index;
int delta = reassembled_chunks_image_data - (index + rescaled_chunk_data);
if (delta < 0) {
result -= Math.abs(delta);
}
return result;
}
添加回答
舉報(bào)
0/150
提交
取消