【java move文件】如何让图片蛇move

posts - 1,&
comments - 24,&
trackbacks - 0
&&&& 摘要: 这两天又做了个Execute Jar Project.觉得以前的Path的知识都忘的差不多了,决定自己总结一下。1. 获取类路径&&&&返回类路径内容,一般为:lib1.lib2.jar。1System.getProperty("java.class.path");&&&&&在命令行运行Java程序时...&&
Daniel 阅读(89) |
2.1-1这题可以参照书上17自己给出过程,这里就略去了。2.1-2 先给出书上insertion-sort的C源代码吧,然后再给出按照非升序的代码:课本中(非降序的)insertion-sort代码:&&在这题中,只要讲非降序改成非升序排序,所以改后代码如下:&&2.1-3这题给出伪代码:&&2.1-4直接给出代码:&&
Daniel 阅读(85) |
优化屏障和内存屏障优化屏障&(Optimization Barrier)编译器编译源代码时,会将源代码进行优化,将源代码的指令进行重排序,以适合于CPU的并行执行。然而,内核同步必须避免指令重新排序,优化屏障(Optimization barrier)避免编译器的重排序优化操作,保证编译程序时在优化屏障之前的指令不会在优化屏障之后执行。Linux用宏barrier实现优化屏障,gcc编译器的优化屏障宏定义列出如下(在include/linux/compiler-gcc.h中):&#define barrier() __asm__ __volatile__("":&:&:"memory")上述定义中,“__asm__”表示插入了汇编语言程序,“__volatile__”表示阻止编译器对该值进行优化,确保变量使用了用户定义的精确地址,而不是装有同一信息的一些别名。“memory”表示指令修改了内存单元。内存屏障 (Memory Barrier)软件可通过读写屏障强制内存访问次序。读写屏障像一堵墙,所有在设置读写屏障之前发起的内存访问,必须先于在设置屏障之后发起的内存访问之前完成,确保内存访问按程序的顺序完成。读写屏障通过处理器构架的特殊指令mfence(内存屏障)、lfence(读屏障)和sfence(写屏障)完成,见《x86-64构架规范》一章。另外,在x86-64处理器中,对硬件进行操作的汇编语言指令是“串行的”,也具有内存屏障的作用,如:对I/O端口进行操作的所有指令、带lock前缀的指令以及写控制寄存器、系统寄存器或调试寄存器的所有指令(如:cli和sti)。Linux内核提供的内存屏障API函数说明如表2。内存屏障可用于多处理器和单处理器系统,如果仅用于多处理器系统,就使用smp_xxx函数,在单处理器系统上,它们什么都不要。
Daniel 阅读(133) |
&&&& 摘要: Preface最近看了一下&Java Concurrency In Practice& 这本书, 总体来说还是一本不错的书, 不过粒度不够细, 是从大的角度, 例如: 设计整体项目上如何考虑并发的多方面因素,不过总体上来说还是一本不错的书,结合部分网络上的资料,总结一下自己的知识,免的忘了。下面是一些最基本的知识,不想再写了,反正网上多的是,挑了一篇还不错的转过来,大家要支持别人的成果...&&
Daniel 阅读(394) |
根据网上的资料做些整理Java NIO API详解这篇文章对nio的api讲解比较全,可以帮助在宏观上把握nio。BIO 方式使得整个处理过程和连接是绑定的,只要连接建立,无论客户端是否有消息发送,都要进行等待处理,一定程度上浪费了服务器端的硬件资源,因此就有了NIO 方式。Java 对于 NIO 方式的支持是通过 Channel和 Selector 方式来实现,采用的方法为向 Channel注册感兴趣的事件,然后通过 Selector 来获取到发生了事件的 key,如发生了相应的事件,则进行相应的处理,否则则不做任何处理,是典型的Reactor 模式,按照这样的方式,就不用像 BIO 方式一样,即使在没有消息的情况下也需要占据一个线程来阻塞读取消息,从而提升服务器的使用效率, 为实现 TCP/IP+NIO 方式的系统间通讯, Java 提供了 SocketChannel和 ServerSocketChannel两个关键的类,网络 IO 的操作则改为通过ByteBuffer 来实现,具体的基于 java实现TCP/IP+NIO 方式的通讯的方法如下所示。服务器端:package com.eric.test.import java.io.IOEimport java.net.InetSocketAimport java.net.ServerSimport java.nio.ByteBimport java.nio.channels.SelectionKimport java.nio.channels.Simport java.nio.channels.ServerSocketCimport java.util.Iimport java.util.Simport java.nio.channels.SocketCpublic class NIOServer {
/*标志数字*/
private static int flag = 0;
/*定义缓冲区大小*/
private static int block = 4096;
/*接收缓冲区*/
private static ByteBuffer receiveBuffer = ByteBuffer.allocate(block);
/*发送缓冲区*/
private static ByteBuffer sendBuffer = ByteBuffer.allocate(block);
/*定义Selector*/
public NIOServer(int port) throws IOException{
//打开服务器套接字通道
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//服务器配置为非阻塞
serverSocketChannel.configureBlocking(false);
//检索与此服务器套接字通道关联的套接字
ServerSocket serverSocket = serverSocketChannel.socket();
//进行服务的绑定
serverSocket.bind(new InetSocketAddress(port));
//通过open()方法找到Selector
selector = Selector.open();
//注册到selector
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Server Start -----8888:");
public void listen() throws IOException{
while(true){
//监控所有注册的 channel ,当其中有注册的 IO 操作可以进行时,该函数返回,并将对应的 SelectionKey 加入 selected-key set
selector.select();
//Selected-key set 代表了所有通过 select() 方法监测到可以进行 IO 操作的 channel ,这个集合可以通过 selectedKeys() 拿到
Set&SelectionKey& selectionKeys = selector.selectedKeys();
Iterator&SelectionKey& iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey selectionKey = iterator.next();
handleKey(selectionKey);
iterator.remove();
//处理请求
public void handleKey(SelectionKey selectionKey) throws IOException{
//接受请求
ServerSocketChannel serverSocketChannel = null;
SocketChannel socketChannel = null;
String receiveT
String sendT
//测试此键的通道是否准备好接受新的套接字连接
if(selectionKey.isAcceptable()){
//返回创建此键的通道
serverSocketChannel = (ServerSocketChannel)selectionKey.channel();
//接受客户端建立连接的请求,并返回 SocketChannel 对象
socketChannel = serverSocketChannel.accept();
//配置为非阻塞
socketChannel.configureBlocking(false);
//注册到selector
socketChannel.register(selector, SelectionKey.OP_READ);
}else if(selectionKey.isReadable()){
//返回为之创建此键的通道
socketChannel = (SocketChannel)selectionKey.channel();
//将缓冲区清空,以备下次读取
receiveBuffer.clear();
//将发送来的数据读取到缓冲区
count = socketChannel.read(receiveBuffer);
if(count&0){
receiveText = new String(receiveBuffer.array(),0,count);
System.out.println("服务器端接受到的数据---"+receiveText);
socketChannel.register(selector, SelectionKey.OP_WRITE);
}else if (selectionKey.isWritable()) {
//将缓冲区清空以备下次写入
sendBuffer.clear();
// 返回为之创建此键的通道。
socketChannel = (SocketChannel) selectionKey.channel();
sendText="message from server--" + flag++;
//向缓冲区中输入数据
sendBuffer.put(sendText.getBytes());
//将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位
sendBuffer.flip();
//输出到通道
socketChannel.write(sendBuffer);
System.out.println("服务器端向客户端发送数据--:"+sendText);
socketChannel.register(selector, SelectionKey.OP_READ);
public static void main(String[] args) throws IOException {
int port = 8888;
NIOServer server = new NIOServer(port);
server.listen();
}}客户端package com.eric.test.import java.io.IOEimport java.net.InetSocketAimport java.nio.ByteBimport java.nio.channels.SelectionKimport java.nio.channels.Simport java.nio.channels.SocketCimport java.util.Spublic class NIOClient {
/*标识数字*/
private static int flag = 0;
/*缓冲区大小*/
private static int BLOCK = 4096;
/*接受数据缓冲区*/
private static ByteBuffer sendBuffer = ByteBuffer.allocate(BLOCK);
/*发送数据缓冲区*/
private static ByteBuffer receiveBuffer = ByteBuffer.allocate(BLOCK);
/*服务器端地址*/
private final static InetSocketAddress SERVER_ADDRESS = new InetSocketAddress(
"localhost", 8888);
public static void main(String[] args) throws IOException {
// 打开socket通道
SocketChannel clientChannel = SocketChannel.open();
// 设置为非阻塞方式
clientChannel.configureBlocking(false);
// 打开选择器
Selector selector = Selector.open();
// 注册连接服务端socket动作
clientChannel.register(selector, SelectionKey.OP_CONNECT);
clientChannel.connect(SERVER_ADDRESS);
SocketChannel socketC
Set&SelectionKey& selectionK
String receiveT
String sendT
int count=0;
while (true) {
//选择一组键,其相应的通道已为 I/O 操作准备就绪。
//监控所有注册的 channel ,当其中有注册的 IO 操作可以进行时,该函数返回,并将对应的 SelectionKey 加入 selected-key set
selector.select();
//返回此选择器的已选择键集。
selectionKeys = selector.selectedKeys();
//System.out.println(selectionKeys.size());
for(SelectionKey selectionKey:selectionKeys){
//判断是否为建立连接的事件
if (selectionKey.isConnectable()) {
System.out.println("client connect");
socketChannel = (SocketChannel) selectionKey.channel();
// 判断此通道上是否正在进行连接操作。
// 完成套接字通道的连接过程。
if (socketChannel.isConnectionPending()) {
//完成连接的建立(TCP三次握手)
socketChannel.finishConnect();
System.out.println("完成连接!");
sendBuffer.clear();
sendBuffer.put("Hello,Server".getBytes());
sendBuffer.flip();
socketChannel.write(sendBuffer);
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
socketChannel = (SocketChannel) selectionKey.channel();
//将缓冲区清空以备下次读取
receiveBuffer.clear();
//读取服务器发送来的数据到缓冲区中
count=socketChannel.read(receiveBuffer);
if(count&0){
receiveText = new String( receiveBuffer.array(),0,count);
System.out.println("客户端接受服务器端数据--:"+receiveText);
socketChannel.register(selector, SelectionKey.OP_WRITE);
} else if (selectionKey.isWritable()) {
sendBuffer.clear();
socketChannel = (SocketChannel) selectionKey.channel();
sendText = "message from client--" + (flag++);
sendBuffer.put(sendText.getBytes());
//将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位
sendBuffer.flip();
socketChannel.write(sendBuffer);
System.out.println("客户端向服务器端发送数据--:"+sendText);
socketChannel.register(selector, SelectionKey.OP_READ);
selectionKeys.clear();
}小结:之前对Selector注册事件和SocketChannel有点小困惑。SocketChannel就像一根水管,当监听到写事件时,就往管道写数据;当监听到读事件时,就从管道读出数据。
Daniel 阅读(225) |
I、关系数据库设计范式介绍1.1 第一范式(1NF)无重复的列&& & & 所谓第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项,同一列中不能有多个值,即实体中的某个属性不能有多个值或者不能有重复的属性。如果出现重复的属性,就可能需要定义一个新的实体,新的实体由重复的属性构成,新实体与原实体之间为一对多关系。在第一范式(1NF)中表的每一行只包含一个实例的信息。简而言之,第一范式就是无重复的列。说明:在任何一个关系数据库中,第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库。&1.2 第二范式(2NF)属性完全依赖于主键[消除部分子函数依赖]&& & & 第二范式(2NF)是在第一范式(1NF)的基础上建立起来的,即满足第二范式(2NF)必须先满足第一范式(1NF)。第二范式(2NF)要求数据库表中的每个实例或行必须可以被惟一地区分。为实现区分通常需要为表加上一个列,以存储各个实例的惟一标识。例如员工信息表中加上了员工编号(emp_id)列,因为每个员工的员工编号是惟一的,因此每个员工可以被惟一区分。这个惟一属性列被称为主关键字或主键、主码。&& & & & 第二范式(2NF)要求实体的属性完全依赖于主关键字。所谓完全依赖是指不能存在仅依赖主关键字一部分的属性,如果存在,那么这个属性和主关键字的这一部分应该分离出来形成一个新的实体,新实体与原实体之间是一对多的关系。为实现区分通常需要为表加上一个列,以存储各个实例的惟一标识。简而言之,第二范式就是属性完全依赖于主键。&1.3 第三范式(3NF)属性不依赖于其它非主属性[消除传递依赖]& & & & & & 满足第三范式(3NF)必须先满足第二范式(2NF)。简而言之,第三范式(3NF)要求一个数据库表中不包含已在其它表中已包含的非主关键字信息。例如,存在一个部门信息表,其中每个部门有部门编号(dept_id)、部门名称、部门简介等信息。那么在的员工信息表中列出部门编号后就不能再将部门名称、部门简介等与部门有关的信息再加入员工信息表中。如果不存在部门信息表,则根据第三范式(3NF)也应该构建它,否则就会有大量的数据冗余。简而言之,第三范式就是属性不依赖于其它非主属性。&II、范式应用实例剖析& & & & 下面以一个学校的学生系统为例分析说明,这几个范式的应用。首先第一范式(1NF):数据库表中的字段都是单一属性的,不可再分。这个单一属性由基本类型构成,包括整型、实数、字符型、逻辑型、日期型等。在当前的任何关系数据库管理系统(DBMS)中,傻瓜也不可能做出不符合第一范式的数据库,因为这些DBMS不允许你把数据库表的一列再分成二列或多列。因此,你想在现有的DBMS中设计出不符合第一范式的数据库都是不可能的。&首先我们确定一下要设计的内容包括那些。学号、学生姓名、年龄、性别、课程、课程学分、系别、学科成绩,系办地址、系办电话等信息。为了简单我们暂时只考虑这些字段信息。我们对于这些信息,说关心的问题有如下几个方面。&学生有那些基本信息学生选了那些课,成绩是什么每个课的学分是多少学生属于那个系,系的基本信息是什么。2.1 第二范式(2NF)实例分析&& & & 首先我们考虑,把所有这些信息放到一个表中(学号,学生姓名、年龄、性别、课程、课程学分、系别、学科成绩,系办地址、系办电话)下面存在如下的依赖关系。&& & & & (学号)→ (姓名, 年龄,性别,系别,系办地址、系办电话)&& & & && (课程名称) → (学分)&& & & & (学号,课程)→ (学科成绩)2.1.1 问题分析&& & & 因此不满足第二范式的要求,会产生如下问题&& & & & 数据冗余: 同一门课程由n个学生选修,"学分"就重复n-1次;同一个学生选修了m门课程,姓名和年龄就重复了m-1次。&& & & & 更新异常:&& & & & & && 1)若调整了某门课程的学分,数据表中所有行的"学分"值都要更新,否则会出现同一门课程学分不同的情况。&& & & & & & 2)假设要开设一门新的课程,暂时还没有人选修。这样,由于还没有"学号"关键字,课程名称和学分也无法记录入数据库。&& & && 删除异常 : 假设一批学生已经完成课程的选修,这些选修记录就应该从数据库表中删除。但是,与此同时,课程名称和学分信息也被删除了。很显然,这也会导致插入异常。2.1.2 解决方案&& & & 把选课关系表SelectCourse改为如下三个表:学生:Student(学号,姓名, 年龄,性别,系别,系办地址、系办电话);课程:Course(课程名称, 学分);选课关系:SelectCourse(学号, 课程名称, 成绩)。2.2 第三范式(3NF)实例分析& & & & 接着看上面的学生表Student(学号,姓名, 年龄,性别,系别,系办地址、系办电话),关键字为单一关键字"学号",因为存在如下决定关系:&& & && (学号)→ (姓名, 年龄,性别,系别,系办地址、系办电话)&& & & & 但是还存在下面的决定关系&& & & &(学号) → (所在学院)→(学院地点, 学院电话)&& & &&& 即存在非关键字段"学院地点"、"学院电话"对关键字段"学号"的传递函数依赖。&& & & & 它也会存在数据冗余、更新异常、插入异常和删除异常的情况。 (數據的更新,刪除異常這里就不分析了,可以參照2.1.1進行分析)& & & & 根据第三范式把学生关系表分为如下两个表就可以滿足第三范式了:&& & & & 学生:(学号, 姓名, 年龄, 性别,系别);&& & & & 系别:(系别, 系办地址、系办电话)。&总结& & & &上面的数据库表就是符合I,II,III范式的,消除了数据冗余、更新异常、插入异常和删除异常。&
Daniel 阅读(91) |
&html& &&& &head& &&& &meta http-equiv="Content-Type" content="text/ charset=gb2312"& &&& &meta name="GENERATOR" content="Microsoft FrontPage 4.0"& &&& &meta name="ProgId" content="FrontPage.Editor.Document"& &&& &title&光标位置&/title& &&& &style& &&& INPUT{border: 1 solid #000000} &&& BODY,TABLE{font-size: 10pt} &&& &/style& &&& &/head& &&& &body& &&& &table border="0" width="700" cellspacing="0" cellpadding="0"& &&& &tr& &&& &td width="479" rowspan="17"& &&& 点击 TextArea 实现光标定位 &&& &p& &&& &input type="Button" value="ff" onclick="fo()"/&& &input type="Button" value="UPLINE" onclick="upLine()"/&& &input type="Button" value="DOWNLINE" onclick="downLine()"/&& &textarea rows="17" cols="49" id="box" onclick="tellPoint()" &HIGHHEELsfsfsfss1123432434asffsasf3232&/textarea&&& &script type="text/javascript"&
& function getPos(textarea) {
& var rangeData = {text: "", start: 0, end: 0 };
& if (textarea.setSelectionRange) { // W3C&
& &textarea.focus();
& &rangeData.start= textarea.selectionS
& &rangeData.end = textarea.selectionE
& &rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end): "";
& } else if (document.selection) { // IE
& &textarea.focus();
& & oS = document.selection.createRange(),
& & // Don't: oR = textarea.createTextRange()
& & oR = document.body.createTextRange();
& &oR.moveToElementText(textarea);
& &rangeData.text = oS.
& &rangeData.bookmark = oS.getBookmark();
& &// object.moveStart(sUnit [, iCount])&
& &// Return Value: Integer that returns the number of units moved.
& &for (i = 0; oR.compareEndPoints('StartToStart', oS) & 0 && oS.moveStart("character", -1) !== 0; i ++) {
& & // Why? You can alert(textarea.value.length)
& & if (textarea.value.charAt(i) == '/r' ) {
& & &i ++;
& &rangeData.start =
& &rangeData.end = rangeData.text.length + rangeData.
& return rangeD
& }&&& function fo(){
&var text = new String(document.getElementById("box").value);
&var currentCurrosr = document.getElementById("pnum").
&alert(text.substring(currentCurrosr-1, currentCurrosr));
&pos = getPos(document.getElementById("box"));
&alert(pos.start);// & & & var end = text.indexOf("\r\n", document.getElementById("pnum").value);// & & &if(end == -1){// & &
alert("END");// & & &}// & & var start = text.lastIndexOf("\r\n", currentCurrosr);// & & if(start == -1){alert("START");}// & & var currentLine = getLine(currentCurrosr);// & & alert("CUL"+currentLine+"L"+currentLine.length);// & & var nextLine = getLine(end + 1);// & & alert("NEXT"+nextLine+"L"+nextLine.length);// & & var preLine = getLine(start - 1);// & & alert("PRE"+ preLine+"L"+preLine.length);& }&&& function upLine(){
&var text = new String(document.getElementById("box").value);
&var currentCurrosr = document.getElementById("h1").& & & var end = text.indexOf("\r\n",currentCurrosr);& & & var start =& & & if(currentCurrosr == end){
start =text.lastIndexOf("\r\n", currentCurrosr-1); && & & }else{& &
start = text.lastIndexOf("\r\n", currentCurrosr);& & & }& & & if(start == -1){alert("Already the toppest!");& & && & & }& & & var preLine = getLine(start-1);& & & var currentLine = getLine(currentCurrosr);& & & var preLineStart = text.lastIndexOf("\r\n", start-1);& & & var sq =& & & var endPart = text.substring(end,text.length);& &
alert(text.length);& & & if(preLineStart == -1){& &
sq = new String(currentLine).substring(2, currentLine.length).concat("\r\n").concat(preLine).concat(endPart);& &
alert(sq);& &
alert(sq.length);& & & }else{& &
&alert(preLineStart);
& & &sq = text.substring(0, preLineStart).concat(currentLine).concat(preLine).concat(endPart);& & & & & alert(sq);& & & & & alert(sq.length);& & & }& & & document.getElementById("box").value=& }&&& function downLine(){
&& }&&& &&&&&& function getLine(currsor){
var text = new String(document.getElementById("box").value);
var end = text.indexOf("\r\n", currsor);
if(end == -1){
return text.substring(text.lastIndexOf("\r\n", text.length), text.length);
var tmp = text.substring(0, end);
var start = tmp.lastIndexOf("\r\n", end);
if(start == -1){
alert(tmp);//
alert(start);//
alert(text.substring(start+4, end).length);//
alert(text.substring(start+4, end));
return text.substring(start, end); }& function movePoint() &&& { &&& var pn=parseInt(pnum.value); &&& if(isNaN(pn)) &&& &&& var rng=box.createTextRange(); &&& rng.moveStart("character",pn); &&& rng.collapse(true); &&& rng.select(); &&& returnCase(rng) &&& } &&& function tellPoint() &&& {&& var rng=event.srcElement.createTextRange(); &&& rng.moveToPoint(event.x,event.y); &&& rng.moveStart("character",-event.srcElement.value.length) &&& h1.value=rng.text.&& pnum.value=rng.text.& returnCase(rng) &&& } &&& && function returnCase(rng) &&& { &&& bh.innerText=rng.boundingH &&& bl.innerText=rng.boundingL &&& bt.innerText=rng.boundingT &&& bw.innerText=rng.boundingW &&& ot.innerText=rng.offsetT &&& ol.innerText=rng.offsetL &&& t.innerText=rng. &&& } &&& && function selectText(sp,ep) &&& { &&& sp=parseInt(sp) &&& ep=parseInt(ep) &&& if(isNaN(sp)||isNaN(ep)) &&& &&& var rng=box.createTextRange(); &&& rng.moveEnd("character",-box.value.length) &&& rng.moveStart("character",-box.value.length) &&& rng.collapse(true); &&& rng.moveEnd("character",ep) &&& rng.moveStart("character",sp) &&& rng.select(); &&& returnCase(rng); &&& } &&& var rg=box.createTextRange(); &&& function findText(tw) &&& { &&& if(tw=="") &&& &&& var sw=0; &&& if(document.selection) &&& { &&& sw=document.selection.createRange().text. &&& } &&& rg.moveEnd("character",box.value.length); &&& rg.moveStart("character",sw); &&& && if(rg.findText(tw)) &&& { &&& rg.select(); &&& returnCase(rg); &&& } &&& if(rg.text!=tw) &&& { &&& alert("已经搜索完了"); &&& rg=box.createTextRange(); &&& } &&& } &&& &/script& &&& &/p& &&& &p&&/p&&&& &input type="hidden" id="h1"& &&& 光标位置:&input type="text" value="0" id="pnum" size="8"& &input type="button" onclick="movePoint()" value="移动光标到指定位置"& &&& &p&&/p& &&& 选择指定范围:&input type="text" size="9" id="sbox"& -- &input type="text" size="9" id="ebox"& &input type="button" onclick="selectText(sbox.value,ebox.value)" value="选择"& &&& &p&&/p& &&& 选择查找字符 :&input type="text" value="" id="cbox" size="8"& &input type="button" onclick="findText(cbox.value)" value="查找下一个并选择"& &&& &/td& &&& &td width="217"&boundingHeight:&&span id="bh"&&/span&&/td& &&& &/tr& &&& &tr& &&& &td width="217"&boundingWidth:&&span id="bw"&&/span&&/td& &&& &/tr& &&& &tr& &&& &td width="217"&boundingTop:&&span id="bt"&&/span&&/td& &&& &/tr& &&& &tr& &&& &td width="217"&boundingLeft:&&span id="bl"&&/span&&/td& &&& &/tr& &&& &tr& &&& &td width="217"&offsetLeft:&&span id="ol"&&/span& &/td& &&& &/tr& &&& &tr& &&& &td width="217"&offsetTop:&&span id="ot"&&/span& &/td& &&& &/tr& &&& &tr& &&& &td width="217"&text:&&span style="position: z-index: 10" id="t" &&/span& &/td& &&& &/tr& &&& &/table& &&& &/body& &&& &/html&
Daniel 阅读(103) |
Daniel 阅读(4091) |
rpc和document的区别&& 18:48:07|&&分类:&字号&
Daniel 阅读(684) |
The project requirements: Should convert TIF into JPG with JAVA, But seems only IMAGEIO can't support TIF.So should use JAI, Java Advantage Image jars.JAI api download page:&AI API document page:&ode as below:package com.ebay.test1;import java.awt.image.RenderedIimport java.io.ByteArrayInputSimport java.io.ByteArrayOutputSimport java.io.Fimport java.io.FileInputSimport java.io.FileNotFoundEimport java.io.FileOutputSimport java.io.IOEimport java.io.InputSimport javax.imageio.ImageIO;import javax.media.jai.JAI;import javax.media.jai.RenderedOp;import org.junit.Timport com.sun.media.jai.codec.ImageCimport com.sun.media.jai.codec.ImageDimport com.sun.media.jai.codec.ImageEimport com.sun.media.jai.codec.JPEGEncodePpublic class Snippet {
@Test public void testTif2JPG2(){
String filename = &"F:\\Downloads\\CCITT_2.TIF";
String jpegFileName = "F:\\Downloads\\CCITT_2.JPG";
RenderedOp source = JAI.create("fileload", filename);
FileOutputStream stream =
stream = new FileOutputStream(jpegFileName );
com.sun.media.jai.codec.JPEGEncodeParam JPEGparam = new
com.sun.media.jai.codec.JPEGEncodeParam();
ImageEncoder encoder =
ImageCodec.createImageEncoder("jpeg",stream,JPEGparam);
encoder.encode(source);
}catch(Exception e){
e.printStackTrace();
@Test public void testTif2JPG3(){
String filename = &"F:\\Downloads\\CCITT_2.TIF";
String jpegFileName = "F:\\Downloads\\CCITT_2.JPG";
FileOutputStream stream =
stream = new FileOutputStream(jpegFileName );
byte[] b = getBytesFromFile(new File(filename));
InputStream bais = new ByteArrayInputStream(b);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", bais, null);& & & & & & RenderedImage ri = decoder.decodeAsRenderedImage();
RenderedOp source = JAI.create("TIFF", bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JPEGEncodeParam JPEGparam = new JPEGEncodeParam();
JPEGparam.setQuality(Float.MIN_VALUE);
ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg",baos,JPEGparam);
encoder.encode(ri);
byte[] byteArray = baos.toByteArray();
FileOutputStream fos = new FileOutputStream(new File(jpegFileName));
fos.write(byteArray);
}catch(Exception e){
e.printStackTrace();
&@Test public void testTif2JPG4(){
String filename = &"F:\\Downloads\\CCITT_2.TIF";
String jpegFileName = "F:\\Downloads\\CCITT_2.JPG";
FileOutputStream stream =
stream = new FileOutputStream(jpegFileName );
byte[] b = getBytesFromFile(new File(filename));
InputStream bais = new ByteArrayInputStream(b);& & & & & & ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", bais, null);& & & & & & RenderedImage ri = decoder.decodeAsRenderedImage();& & & & & & ByteArrayOutputStream outputStream = new ByteArrayOutputStream();& & & & & & ImageIO.write(ri, "JPEG", outputStream);& & & & & & stream.write(outputStream.toByteArray());
}catch(Exception e){
e.printStackTrace();
} } // Returns the contents of the file in a byte array.& & public static byte[] getBytesFromFile(File file) throws IOException {& & & & InputStream is = new FileInputStream(file);& &&& & & & // Get the size of the file& & & & long length = file.length();& &&& & & & // You cannot create an array using a long type.& & & & // It needs to be an int type.& & & & // Before converting to an int type, check& & & & // to ensure that file is not larger than Integer.MAX_VALUE.& & & & if (length & Integer.MAX_VALUE) {& & & &
throw new IllegalArgumentException("File is too big, can't support.");& & & & }& &&& & & & // Create the byte array to hold the data& & & & byte[] bytes = new byte[(int)length];& &&& & & & // Read in the bytes& & & & int offset = 0;& & & & int numRead = 0;& & & & while (offset & bytes.length& & & & & & & &&& (numRead=is.read(bytes, offset, bytes.length-offset)) &= 0) {& & & & & & offset += numR& & & & }& &&& & & & // Ensure all the bytes have been read in& & & & if (offset & bytes.length) {& & & & & & throw new IOException("Could not completely read file "+file.getName());& & & & }& &&& & & & // Close the input stream and return bytes& & & & is.close();& & & && & } }
Daniel 阅读(1133) |
303112345678910111213141516171819202122232425262728293012345678910}

我要回帖

更多关于 java贪吃蛇教程 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信