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;
}
}
原型:
int WINAPI icePub_imgToFileTextdata(char *strImgFilename,char *strFilename,char *strFenge,int flag)
输入:strImgFilename 待处理图像文件 (会被强制256级灰度化)
strFilename 待生成BMP文本数据文件名
strFenge 列之间分隔符
flag 文本数据格式标志:0 10进制; 1 16进制; 10 RGB16进制
输出:
VB sample 代码:
Private Declare Function icePub_imgToFileTextdata Lib "icePubDll.dll" (ByVal strImgFilename As String, ByVal strFilename As String, ByVal strFenge As String, ByVal flag As Integer) As Integer
Dim a2 As Long
a2 = icePub_imgToFileTextdata("a.jpg","bmpdata.txt",",",0)
VC sample代码:
extern "C"
{
__declspec(dllexport)
int WINAPI icePub_imgToFileTextdata(char *strImgFilename,char *strFilename,char *strFenge,int flag);
}
#pragma comment(lib,"icePubDll.lib")
icePub_imgToFileTextdata("a.jpg","bmpdata.txt",",",0);
重载渲染控件的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,也可以直接设置仿射变换矩阵,利用这点就可以根据所需要的实现方式来进行描绘.