java 圖片與字符串的轉換


	/**
	 * 將圖片文件轉為字符串
	 * @param imgFile
	 * @return
	 */
	public static String getImageStr(String imgFile) {
		// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
		//String imgFile = "d:\\111.jpg";// 待處理的圖片
		InputStream in = null;
		byte[] data = null;
		// 讀取圖片字節數組
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 對字節數組Base64編碼
		BASE64Encoder encoder = new BASE64Encoder();
		// 返回Base64編碼過的字節數組字符串
		return encoder.encode(data);
	}
	
	/**
	 * 將圖片文件轉為byte數字
	 * @param imgFile
	 * @return
	 */
	public static byte[] getImageByte(String imgFile) {
		// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
		//String imgFile = "d:\\111.jpg";// 待處理的圖片
		InputStream in = null;
		byte[] data = null;
		// 讀取圖片字節數組
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 返回Base64編碼過的字節數組字符串
		return data;
	}

	/**
	 * 將字符串轉為圖片
	 * @param imgStr
	 * @return
	 */
	public static boolean generateImage(String imgStr,String imgFile)throws Exception {
		// 對字節數組字符串進行Base64解碼並生成圖片
		if (imgStr == null) // 圖像數據為空
			return false;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解碼
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 調整異常數據
					b[i] += 256;
				}
			}
			// 生成jpeg圖片
			String imgFilePath = imgFile;// 新生成的圖片
			OutputStream out = new FileOutputStream(imgFilePath);
			out.write(b);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			throw e;
		}
	}
	
	/**
	 * 圖片是否符合 jpg gjf png格式
	 * @param imgStr
	 * @return
	 */
	public static boolean isRightFormat(String format){

		return (format.equals("jpg") || format.equals("gif") || format.equals("png"))?true:false;
	}

	 /**
     * 對圖片進行放大
     * @param originalImage 原始圖片
     * @param times 放大倍數
     * @return
     */
	
	public static BufferedImage  zoomInImage(BufferedImage  originalImage, Integer times){
		
		int width = originalImage.getWidth()*times;
		int height = originalImage.getHeight()*times;
		
		BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());
		Graphics g = newImage.getGraphics();
		g.drawImage(originalImage, 0,0,width,height,null);
		g.dispose();
		return newImage;
	}
	
	/**
     * 對圖片進行放大
     * @param srcPath 原始圖片路徑(絕對路徑)
     * @param newPath 放大后圖片路徑(絕對路徑)
     * @param times 放大倍數
     * @return 是否放大成功
     */
	
	public static boolean zoomInImage(String srcPath,String newPath,Integer times){
		
		BufferedImage bufferedImage = null;
		try{
			
			File of = new File(srcPath);
			if(of.canRead()){
				
				bufferedImage =  ImageIO.read(of);
				
			}
		}catch(Exception e){
			//TODO: 打印日志
			return false;
		}
		if(bufferedImage != null){
			
			 bufferedImage = zoomInImage(bufferedImage,times);
		       try {
	                //TODO: 這個保存路徑需要配置下子好一點
		    	    //保存修改后的圖像,全部保存為JPG格式
	                ImageIO.write(bufferedImage, "JPG", new File(newPath)); 
	            } catch (IOException e) {
	                // TODO 打印錯誤信息
	                return false;
	            }
		}
		return true;
		
	}
	
	 /**
     * 對圖片進行縮小
     * @param originalImage 原始圖片
     * @param times 縮小倍數
     * @return 縮小后的Image
     */
    public static BufferedImage  zoomOutImage(BufferedImage  originalImage, Integer times){
        int width = originalImage.getWidth()/times;
        int height = originalImage.getHeight()/times;
        BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0,0,width,height,null);
        g.dispose();
        return newImage;
    }

    /**
     * 對圖片進行放大
     * @param srcPath 原始圖片路徑(絕對路徑)
     * @param newPath 放大后圖片路徑(絕對路徑)
     * @param times 放大倍數
     * @return 是否放大成功
     */
	
    public static boolean zoomOutImage(String srcPath,String newPath,Integer times){
        BufferedImage bufferedImage = null;
        try {
            File of = new File(srcPath);
            if(of.canRead()){
                bufferedImage =  ImageIO.read(of);
            }
        } catch (IOException e) {
            //TODO: 打印日志
            return false;
        }
        if(bufferedImage != null){
            bufferedImage = zoomOutImage(bufferedImage,times);
            try {
                //TODO: 這個保存路徑需要配置下子好一點
            	//保存修改后的圖像,全部保存為JPG格式
                ImageIO.write(bufferedImage, "JPG", new File(newPath)); 
            } catch (IOException e) {
                // TODO 打印錯誤信息
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        boolean testIn = zoomInImage("E:/11.jpg","E:\\in.jpg",4);
        if(testIn){
            System.out.println("in ok");
        }
        boolean testOut = zoomOutImage("E:/11.jpg","E:\\out.jpg",4);
        if(testOut){
            System.out.println("out ok");
        }
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM