}
//抽象工厂------例子2(文件读取)
/**
要求:
编写读文件功能。具体功能是:读取文本文件,包括(GBK,UTF-8,UNICoDe)编码下的文本文件,要求获得全文内容;
获取图像文件(BMP,GIF,JPG)文件,要求获得图像宽度、长度、每一点的RGB三基色信息。
定义泛型接口是解决返回值类型不同的较好方法,屏蔽方法参数个数差异利用“String ... in”形式实现。
*/
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
interface IRead<T>{
T read(String ... in);
}
class FileRead implements IRead<String>{
public String read(String ... in){ //可以传入多个参数
String result = null;
try{
File file = new File(in[0]); //in[0]表示文件名
int len = 0;
FileInputStream input = new FileInputStream(file);
byte buf[] = new byte[1024]; //缓冲区大小等于文件长度
// FileOutputStream output = new FileOutputStream("");
// while((len=input.read(buf)) != -1){
// output.write(buf,0,len);
// }
input.read(buf); //一次性读完文件长度
result = new String(buf,in[1]); //按in[1]编码方式转换成课件字符串
input.close();
}catch(Exception e){
e.getMessage();
}
return result;
}
}
class ImageInfo{
private int width;
private int height;
private int r[][];
private int g[][];
private int b[][];
public void setWidth(int width){
this.width = width;
}
public void setHeight(int height){
this.height = height;
}
public int[][] getR(){
return r;
}
public int[][] getG(){
return g;
}
public int[][] getB(){
return b;
}
public int getHeight() {
// TODO Auto-generated method stub
return height;
}
public int getWidth() {
// TODO Auto-generated method stub
return width;
}
public void setRGB(int rgb[]){
r = new int[height][width];
g= new int[height][width];
b= new int[height][width];
int pos = 0;
for(int i =0;i<height;i++){
pos = width * i;
for(int j =0;j<width;j++){ //这里是位运算的操作。
r[i][j] = (rgb[pos+j]&0xff0000)>>16;
g[i][j] = (rgb[pos+j]&0x00ff00)>>8;
b[i][j] = rgb[pos + j]&0x0000ff;
}
}
}
public String toString(){
return "width:"+this.getWidth()+" height:"+this.getHeight()+" r:"+this.getR()+" g:"+this.getG()+" b:"+this.getB();
}
}
class ImageRead implements IRead<ImageInfo>{ //读图像文件
BufferedImage bi = null;
public ImageInfo read(String ... in){
File file = new File(in[0]);
//in[0]表示图像的文件名
try{
/**
static BufferedImage read(File input)
返回一个 BufferedImage,作为使用 ImageReader(它是从当前已注册 ImageReader 中自动选择的)解码所提供 File 的结果。
*/
bi = ImageIO.read(file);
}catch(Exception e){
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int rgb[] = new int[width * height];
//将数据图像读到result缓冲区
/**
int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
从图像数据的某一部分返回默认 RGB 颜色模型 (TYPE_INT_ARGB) 和默认 sRGB 颜色空间中整数像素数组。 */
bi.getRGB(0,0,width,height,rgb,width,height);
ImageInfo obj= new ImageInfo(); //设置图像信息
obj.setWidth(width);
obj.setHeight(height);
obj.setRGB(rgb);
return obj;
}
}
//自动选择工厂
abstract class AbstractFactory{
private static final String TEXT = "text";
private static final String IMAGE = "image";
abstract IRead creat();
static AbstractFactory creat(String mark){
AbstractFactory factory = null;
if(mark.equals(TEXT)){ //相当于:AbstractFactory factory = new TextFactory();
factory = new TextFactory();
}else if(mark.equals(IMAGE)){
factory = new ImageFactory();
}
return factory;
}
}
class TextFactory extends AbstractFactory{
public IRead creat(){
//IRead ifileread = new FileRead();
return new FileRead();
}
}
class ImageFactory extends AbstractFactory{
public IRead creat(){
return new ImageRead();
}
}
public class ReadInfoDemo{
public static void main(String []args){
String result = (String)AbstractFactory.creat("text").creat().read("F://JavaOracle/OOP/AbstractDemos.java","GBK");
//TextFactory tf = AbstractFactory.creat("text");
// IRead ifread = tf.creat();
//String result = ifread.read("F:/JavaOracle/OOP/AbstractDemos.java","GBK");
System.out.println("读取文件的信息:"+result);
ImageInfo ir = (ImageInfo)AbstractFactory.creat("image").creat().read("F://JavaOracle/18.png");
System.out.println("读取图片的信息:"+ir);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011521890/article/details/46761747