
正文
java写照片转代码 java写图片
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
怎么用JAVA写图片格式转换,可以批量转换的。
书写完毕,采纳即可。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
public class YuGiOh
{
public static final String SEPARATOR = System.getProperty ("file.separator");
public static final String LINE = System.getProperty ("line.separator");
/**
* 转换之后保存的路径
*/
private static final String SAVE = "convertSuffix";
/**
* 递归读取文件夹中的特定后缀的文件
*
* @param path
* String 读取的文件夹路径
* @param suffix
* String 后缀名,|分隔
* @param newSuffix
* String 新的后缀名
*/
public static void convertSuffix ( String path, final String suffix, final String newSuffix )
{
File p = new File (path);
String name = p.getName (), regex = "(?i)([^\\.]*)\\.(" + suffix + ")";
if (p.isDirectory ())
{
p.list (new FilenameFilter ()
{
@Override
public boolean accept ( File dir, String name )
{
if (dir.isDirectory ())
{
convertSuffix (dir.getAbsolutePath () + SEPARATOR + name, suffix, newSuffix);
}
return false;
}
});
}
else if (name.matches (regex))
{
saveFiles (path, name, newSuffix);
}
}
/**
* 读取到特定的后缀,修改后缀,保存文件
*
* @param path
* String 读取的文件夹路径
* @param name
* String 特定的后缀的文件名
* @param newSuffix
* String 新的后缀名
*/
public static void saveFiles ( String path, String name, String newSuffix )
{
try
{
File fp = new File (SAVE);
if (!fp.exists ())
{
fp.mkdir ();
}
name = name.replaceAll ("([^\\.]+)(\\..*)?", "$1." + newSuffix);
InputStream is = new FileInputStream (path);
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
byte[] buffer = new byte[1024];
int len = -1;
while (( len = is.read (buffer) ) != -1)
{
baos.write (buffer, 0, len);
}
baos.flush ();
baos.close ();
is.close ();
byte[] data = baos.toByteArray ();
FileOutputStream fos = new FileOutputStream (new File (SAVE + SEPARATOR + name));
fos.write (data);
fos.flush ();
fos.close ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
public static void main ( String[] args )
{
convertSuffix ("F:\\Photo\\pic", "png|jpg", "gif");
}
}
相关问答
Q1: java如何把图片转换成二进制并存到oracle的blob中,求代码
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class ImageUtils {
public static void main(String[] args) {
String str = img2Binary("C:\\Users\\hny\\Desktop\\favicon.jpg");
System.out.println(str);
binary2Img("C:\\Users\\hny\\Desktop\\favicon2.jpg", str);
}
/**
* 图片转二进制字符串
*
* @param path 图片路径
* @return
*/
public static String img2Binary(String path) {
File file = new File(path);
if (!file.exists()) {
return null;
}
try {
BufferedImage bi = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String suffix = getSuffix(path);
ImageIO.write(bi, suffix, baos);
byte[] bytes = baos.toByteArray();
return new sun.misc.BASE64Encoder().encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 字符串转图片文件
*
* @param path 图片路径
* @param imgBinary 图片字符串
*/
public static void binary2Img(String path, String imgBinary) {
try {
File file = new File(path);
byte[] bytes1 = new sun.misc.BASE64Decoder().decodeBuffer(imgBinary);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
String suffix = getSuffix(path);
ImageIO.write(bi1, suffix, file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取图片后缀名
*
* @param path
* @return
*/
private static String getSuffix(String path) {
int index = path.contains(".") ? path.lastIndexOf(".") : -1;
if (index -1) {
return path.substring(index + 1);
}
return null;
}
}
Q2: 利用java实现图片翻转的代码
重载渲染控件java写照片转代码的paintComponent(Graphics
g)方法.
设java写照片转代码你当前图像实例为img,已初始化,需要旋转java写照片转代码的角度为ang
public
void
paintComponent(Graphics
g){
super.paintCompoent(g);
Graphics2D
g2d
=
(Graphics2D)g;
g2d.rotate(-angle);
g2d.drawImage(img,0,0,this.getWidth(),this.getHeight(),null);
}
Graphics,Graphics2D
类中有对当前描绘环境进行仿射变换java写照片转代码的方法,包括translate,scale,rotate,也可以直接设置仿射变换矩阵,利用这点就可以根据所需要的实现方式来进行描绘.
关于java写照片转代码和java写图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








