Skip to content

Commit 919fc4f

Browse files
committed
1.24
1 parent 66998cf commit 919fc4f

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
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.23'
9+
version '1.24'
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.23";
4+
protected static final String VERSION = "1.24";
55

66
protected static final int NATIVE_LIB_VERSION = 2;
77
}

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public static void inputToOutputStream(InputStream in, OutputStream out) throws
105105
*/
106106
public static boolean isImage(byte[] data) {
107107
try {
108-
ImageIO.read(new ByteArrayInputStream(data));
108+
ImageIO.read(new ByteArrayInputStream(data)).getType();
109109
return true;
110-
} catch (IOException e) {
110+
} catch (IOException | NullPointerException e) {
111111
return false;
112112
}
113113
}
@@ -121,18 +121,31 @@ public static boolean isImage(byte[] data) {
121121
* @throws IOException exception
122122
*/
123123
public static BufferedImage reductionSize(BufferedImage image, long size) throws IOException {
124-
long lastSize = toByteArray(image, "png").length;
124+
return reductionSize(image, size, "png");
125+
}
126+
127+
/**
128+
* 画像を指定サイズまで縮小する
129+
*
130+
* @param image 対象画像
131+
* @param size サイズ
132+
* @param formatName フォーマット名
133+
* @return 縮小済み
134+
* @throws IOException exception
135+
*/
136+
public static BufferedImage reductionSize(BufferedImage image, long size, String formatName) throws IOException {
137+
long lastSize = toByteArray(image, formatName).length;
125138
if (lastSize <= size) return image;
126139
float scale = (float) size / lastSize;
127140
BufferedImage nimg = resize(image, (int) (image.getWidth() * scale), (int) (image.getHeight() * scale));
128-
return reductionSizeW(nimg, size, lastSize);
141+
return reductionSizeW(nimg, size, lastSize, formatName);
129142
}
130143

131-
private static BufferedImage reductionSizeW(BufferedImage image, long size, long lastSize) throws IOException {
132-
byte[] data = toByteArray(image, "png");
144+
private static BufferedImage reductionSizeW(BufferedImage image, long size, long lastSize, String formatName) throws IOException {
145+
byte[] data = toByteArray(image, formatName);
133146
if (data.length == lastSize) throw new IOException("Can't be smaller than this");
134147
if (data.length <= size) return image;
135148
BufferedImage nimg = resize(image, image.getWidth() / 2, image.getHeight() / 2);
136-
return reductionSizeW(nimg, size, data.length);
149+
return reductionSizeW(nimg, size, data.length, formatName);
137150
}
138151
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package dev.felnull;
22

3+
import dev.felnull.fnjl.util.FNDataUtil;
34
import dev.felnull.fnjl.util.FNImageUtil;
45
import dev.felnull.fnjl.util.FNStringUtil;
6+
import dev.felnull.fnjl.util.FNURLUtil;
57

6-
import javax.imageio.ImageIO;
7-
import java.awt.image.BufferedImage;
8-
import java.io.File;
8+
import java.net.URL;
99

1010
public class TestMain {
1111
public static void main(String[] args) throws Exception {
1212
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"));
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+
boolean im = FNImageUtil.isImage(FNDataUtil.streamToByteArray(FNURLUtil.getStream(new URL("https://pbs.twimg.com/media/FHXlg_QacAMfISG?format=png&name=medium"))));
16+
System.out.println(im);
1517
//System.out.println(FNMath.scale(512, 1024));
1618
/* long time = System.currentTimeMillis();
1719
FNRuntimeUtil.multipleRun("Ikisugi", 2, () -> {

0 commit comments

Comments
 (0)