图片网络传输

1、总体描述

(1)图片压缩

(2)读取压缩后的图片文件,base64进行编码,拼接xml字符串

(3)使用http传输xml

(4)接收端解析xml,还原图片文件

 

 

2、图片压缩demo

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageCompress {
	public static void ImageScale(String path, String fileName,	String toFileName) {
		try {
			
			Image image = javax.imageio.ImageIO.read(new File(path + fileName));
			int imageWidth = image.getWidth(null);
			int imageHeight = image.getHeight(null);
			//计算缩放比例
			float scale = getRatio(imageWidth, imageHeight, 130, 130);
			imageWidth = (int) (scale * imageWidth);
			imageHeight = (int) (scale * imageHeight);

			image = image.getScaledInstance(imageWidth, imageHeight,Image.SCALE_AREA_AVERAGING);
			// Make a BufferedImage from the Image.
			BufferedImage mBufferedImage = new BufferedImage(imageWidth,imageHeight, BufferedImage.TYPE_INT_RGB);
			//获取图形上下文
			Graphics2D g2 = mBufferedImage.createGraphics();

			//绘制图像到目标位置
			g2.drawImage(image, 0, 0, imageWidth, imageHeight, Color.white,	null);
			g2.dispose();

			float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,-0.125f, -0.125f, -0.125f, -0.125f };
			Kernel kernel = new Kernel(3, 3, kernelData2);
			//使用卷积核的卷积是一种通过输入像素来计算输出像素的空间运算,方法是将核与输入像素邻域相乘。这种运算使得直接邻域可按核数学指定的方式影响输出像素
			//ConvolveOp.EDGE_NO_OP将位于源图像边缘的像素复制为目标中相应的像素,不加修改。
			//构造给定 Kernel、边缘条件和 RenderingHint 对象(可以为 null)的 ConvolveOp。
			ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
			//对 BufferedImage 执行卷积运算。
			mBufferedImage = cOp.filter(mBufferedImage, null);

			FileOutputStream out = new FileOutputStream(path + toFileName);
			//可以正常实现bmp转jpg、png转jpg、gif转jpg
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(mBufferedImage);
			out.close();
		} catch (FileNotFoundException fnf) {
			fnf.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public static float getRatio(int width, int height, int maxWidth,int maxHeight) {
		float Ratio = 1.0f;
		float widthRatio;
		float heightRatio;
		widthRatio = (float) maxWidth / width;
		heightRatio = (float) maxHeight / height;
		if (widthRatio < 1.0 || heightRatio < 1.0) {
			Ratio = widthRatio <= heightRatio ? widthRatio : heightRatio;
		}
		return Ratio;
	}

	public static void main(String[] args) {
		ImageScale("", "SNV31539.JPG", "SNV31539_small.JPG");
	}
}

 

 3、读取压缩后的图片文件,base64进行编码,拼接xml字符串

 

public static String bin2XmlString(File file) {
		byte[] data = null;
		FileInputStream input = null;
		String ret = null;
		int n;
		try {
			data = new byte[(int) file.length()];
			input = new FileInputStream(file);
			n = input.read(data);
			input.close();
			ret = new String(Base64.encodeBase64(data, true));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

 

4、接收端解析xml,还原图片文件

 

public static boolean xmlString2Bin(String base64String, File file) {
		byte[] data;
		FileOutputStream output = null;
		boolean ret = false;
		try {
			data = Base64.decodeBase64(base64String.getBytes());
			output = new FileOutputStream(file);
			output.write(data);
			output.close();
			ret = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

 

 

你可能感兴趣的:(java,xml,sun)