工厂模式设计实现读取文本文件和图像的三基色信息
来源:互联网 发布:java后台和php后台 编辑:程序博客网 时间:2024/06/11 18:43
要求:
编写读文件功能,具体为读取文本文件(包括GBK,UTF8,UNICODE 编码下的文本文件,要求获得全文内容);读取图像文件(BMO,GIF,JPG ),要求获得图像的宽度、长度、每一点的RGB三基色信息。
请使用观察者模式设计该系统。
代码:
import java.awt.image.BufferedImage;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import javax.imageio.ImageIO;import javax.swing.ImageIcon;interface Type{public void read();public void read(String filePath);}class TextType implements Type{public void read(String filePath){try{String encoding="GBK";File file = new File(filePath);if(file.isFile()&&file.exists()){InputStreamReader du = new InputStreamReader(new FileInputStream(file),encoding);BufferedReader bufferedReader = new BufferedReader(du);String lineTxt = null;while((lineTxt = bufferedReader.readLine())!=null){System.out.println(lineTxt);}du.close();}else{System.out.println("找不到指定文件!");}}catch(Exception e){System.out.println("读取文件内容出错!");e.printStackTrace();}}@Overridepublic void read() {// TODO Auto-generated method stub}}class ImageType implements Type{public void read(){}@Overridepublic void read(String filePath) { ImageIcon image=new ImageIcon(filePath); InputStream in; BufferedImage img = null;try {in = new FileInputStream(filePath);img=ImageIO.read(in);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch(IOException e){e.printStackTrace();} int wid=img.getWidth(); int hid=img.getHeight(); int rgb[][]=new int[wid][hid]; for(int i=0;i<wid;i++){ for(int j=0;j<hid;j++){ rgb[i][j]=img.getRGB(i, j) & 0xFFFFFF; System.out.print(rgb[i][j]+" "); } System.out.println(); } //System.out.println(wid+" "+hid); } }class TypeFactory{public Type getType(String type){if(type==null){return null;}if(type.equals("TextType")){return new TextType();}else if(type.equals("ImageType")){return new ImageType();}return null;}}public class FactoryDemo {public static void main(String []args){TypeFactory typefactory = new TypeFactory();Type type1 = typefactory.getType("TextType");String filePath1 ="C:\\Users\\ZZT\\Desktop\\1.txt";type1.read(filePath1);System.out.println();Type type2 = typefactory.getType("ImageType");String filePath2 ="C:\\Users\\ZZT\\Desktop\\2.jpg";type2.read(filePath2);}}
0 0
- 工厂模式设计实现读取文本文件和图像的三基色信息
- Java 实现工厂设计模式的三种方式
- 三基色、三原色的区别
- 简单工厂,工厂方法,抽象工厂 三种设计模式的区别和实例
- 设计模式(三) 抽象工厂的学习和使用
- 设计模式之三:工厂方法模式—多态工厂的实现
- [Gof设计模式]简单工厂模式和工厂模式的C++实现
- 三基色原理与色彩模式
- SSH中用到的设计模式之三——工厂方法和抽象工厂
- 设计模式--工厂模式(三)
- 设计模式(三)--工厂模式
- 【设计模式 三】工厂模式
- 设计模式和工厂模式的结合
- 三种工厂模式的C++实现
- 三种工厂模式的C++实现
- 三种工厂模式的C++实现
- 三种工厂模式的实现
- 三种工厂模式的C++实现
- Fragment的添加和删除及其源码分析
- 83、myeclipse 中 svn 更新 提交 同步资源库 详细解释下他们的功能
- hibernate lazy属性参数说明
- JavaScript数据类型、类与对象
- leetcode27
- 工厂模式设计实现读取文本文件和图像的三基色信息
- 网页中图片的宽度
- 微服务实战(六):选择微服务部署策略
- Spark性能优化:数据倾斜调优
- Memcached的MemCachedClient设置过期时限
- Java反射实现不同对象相同代码地对象查询和封装
- mysql常用配置---详细解释
- tomcat服务启动与停止的监听
- 微服务实践(七):从单体式架构迁移到微服务架构