书写完毕,采纳即可。
创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业服务器主机托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。
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");
}
}
Java中将图片转为二进制流只需要使用FileImageInputStream取得图片文件,然后使用ByteArrayOutputStream 写入到二进制流中即可,下面是详细代码:
//图片到byte数组
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
另外,如果需要将byte[]存回图片或转为String,则:
//byte数组到图片
public void byte2image(byte[] data,String path){
if(data.length3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
public String byte2string(byte[] data){
if(data==null||data.length=1) return "0x";
if(data.length200000) return "0x";
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte数组转化成十进制
for(int k=0;kdata.length;k++){
buf[k] = data[k]0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(int k=0;kbuf.length;k++){
if(buf[k]16) sb.append("0"+Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return "0x"+sb.toString().toUpperCase();
}
重载渲染控件的paintComponent(Graphics
g)方法.
设你当前图像实例为img,已初始化,需要旋转的角度为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
类中有对当前描绘环境进行仿射变换的方法,包括translate,scale,rotate,也可以直接设置仿射变换矩阵,利用这点就可以根据所需要的实现方式来进行描绘.
#首先在D盘写一个文件"temp.html",如下内容
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
title图片转文本/title
meta http-equiv="content-type" content="text/html; charset=gbk"
style type="text/css"
body {
font-family: 宋体; line-height: 0.8em; letter-spacing: 0px; font-size: 8px;
}
/style
/head
body
${content}
/body
/html
#在D盘放一个图片(放小一点的)"a.jpg"
#运行如下JAVA代码:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class Test {
/** 此处设置灰度字符,此处只用十个字符,可以设置更多 */
private static char[] cs = new char[] { '.', ',', '*', '+', '=', '', '$', '@', '#', ' ' };
public static void main(String[] args) throws IOException {
// 读取图片
BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"));
// 图片转字符串后的数组
char[][] css = new char[bfedimage.getWidth()][bfedimage.getHeight()];
for (int x = 0; x bfedimage.getWidth(); x++) {
for (int y = 0; y bfedimage.getHeight(); y++) {
int rgb = bfedimage.getRGB(x, y);
Color c = new Color(rgb);
// 得到灰度值
int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
css[x][y] = cs[(int) ((cc * 10 - 1) / 255)];
}
}
// 取得模板HTML
String temp = readFile(new File("D:\\temp.html"),"gbk");
StringBuffer sb = new StringBuffer();
// 开始拼接内容
for (int y = 0; y css[0].length; y++) {
for (int x = 0; x css.length; x++) {
sb.append(css[x][y]);
}
sb.append("\r\n");
}
System.out.println(sb.toString());
// 生成文件
String content = toHTML(sb.toString());
String filecontent = replaceStrAllNotBack(temp, "${content}", content);
writeFile(new File("D:\\content.html"), filecontent, "gbk");
}
public static String toHTML(String s) {
s = s.replaceAll("", "");
s = s.replaceAll(" ", " ");
s = s.replaceAll("", "");
s = s.replaceAll("", "");
s = s.replaceAll("\"", """);
s = s.replaceAll("\\\r\\\n", "br/");
s = s.replaceAll("\\\r", "br/");
s = s.replaceAll("\\\n", "br/");
return s;
}
public static String replaceStrAllNotBack(String str, String strSrc, String strDes) {
StringBuffer sb = new StringBuffer(str);
int index = 0;
while ((index = sb.indexOf(strSrc, index)) != -1) {
sb.replace(index, index + strSrc.length(), strDes);
index += strDes.length();
}
return sb.toString();
}
/**
* 读文件(使用默认编码)
*
* @param file
* @return 文件内容
* @throws IOException
*/
public static String readFile(File file, String charset) throws IOException {
InputStreamReader fr = new InputStreamReader(new FileInputStream(file), charset);
StringBuffer sb = new StringBuffer();
char[] bs = new char[1024];
int i = 0;
while ((i = fr.read(bs)) != -1) {
sb.append(bs, 0, i);
}
fr.close();
return sb.toString();
}
/**
* 写文件
*
* @param file
* @param string
* 字符串
* @param encoding
* 编码
* @return 文件大小
* @throws IOException
*/
public static int writeFile(File file, String string, String encoding) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
byte[] bs = string.getBytes(encoding);
fos.write(bs);
return bs.length;
} finally {
fos.close();
}
}
}
#打开"D:\content.html"文件看效果吧。
有什么问题可以联系我。