Skip to content

Commit 66998cf

Browse files
committed
1.23
1 parent 3ea1b8b commit 66998cf

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
group 'dev.felnull'
88
archivesBaseName = "felnull-java-library"
9-
version '1.22'
9+
version '1.23'
1010

1111
repositories {
1212
mavenCentral()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.felnull.fnjl;
22

33
public class BuildIn {
4-
protected static final String VERSION = "1.22";
4+
protected static final String VERSION = "1.23";
55

66
protected static final int NATIVE_LIB_VERSION = 2;
77
}

src/main/java/dev/felnull/fnjl/util/FNImageUtil.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,43 @@ public static void inputToOutputStream(InputStream in, OutputStream out) throws
9696
out.write(buffer, 0, len);
9797
}
9898
}
99+
100+
/**
101+
* バイト配列が画像かどうか
102+
*
103+
* @param data 検体
104+
* @return 画像かどうか
105+
*/
106+
public static boolean isImage(byte[] data) {
107+
try {
108+
ImageIO.read(new ByteArrayInputStream(data));
109+
return true;
110+
} catch (IOException e) {
111+
return false;
112+
}
113+
}
114+
115+
/**
116+
* 画像を指定サイズまで縮小する
117+
*
118+
* @param image 対象画像
119+
* @param size サイズ
120+
* @return 縮小済み
121+
* @throws IOException exception
122+
*/
123+
public static BufferedImage reductionSize(BufferedImage image, long size) throws IOException {
124+
long lastSize = toByteArray(image, "png").length;
125+
if (lastSize <= size) return image;
126+
float scale = (float) size / lastSize;
127+
BufferedImage nimg = resize(image, (int) (image.getWidth() * scale), (int) (image.getHeight() * scale));
128+
return reductionSizeW(nimg, size, lastSize);
129+
}
130+
131+
private static BufferedImage reductionSizeW(BufferedImage image, long size, long lastSize) throws IOException {
132+
byte[] data = toByteArray(image, "png");
133+
if (data.length == lastSize) throw new IOException("Can't be smaller than this");
134+
if (data.length <= size) return image;
135+
BufferedImage nimg = resize(image, image.getWidth() / 2, image.getHeight() / 2);
136+
return reductionSizeW(nimg, size, data.length);
137+
}
99138
}

src/test/java/dev/felnull/TestMain.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package dev.felnull;
22

3-
import dev.felnull.fnjl.util.FNMath;
3+
import dev.felnull.fnjl.util.FNImageUtil;
4+
import dev.felnull.fnjl.util.FNStringUtil;
5+
6+
import javax.imageio.ImageIO;
7+
import java.awt.image.BufferedImage;
8+
import java.io.File;
49

510
public class TestMain {
611
public static void main(String[] args) throws Exception {
7-
System.out.println(FNMath.scale(512, 1024));
12+
System.out.println(FNStringUtil.getByteDisplay(1000 * 1000));
13+
BufferedImage img = FNImageUtil.reductionSize(ImageIO.read(new File("V:\\dev\\java\\FelNullJavaLibrary\\test\\t.png")), 1000 * 1000);
14+
ImageIO.write(img, "png", new File("V:\\dev\\java\\FelNullJavaLibrary\\test\\t2.png"));
15+
//System.out.println(FNMath.scale(512, 1024));
816
/* long time = System.currentTimeMillis();
917
FNRuntimeUtil.multipleRun("Ikisugi", 2, () -> {
1018
try {

0 commit comments

Comments
 (0)