@@ -1,244 +0,0 @@
package sc545.pay.utils ;
import java.awt.image.BufferedImage ;
import java.io.ByteArrayOutputStream ;
import java.io.File ;
import java.io.IOException ;
import java.io.InputStream ;
import java.net.URL ;
import java.util.* ;
import javax.imageio.ImageIO ;
import net.coobird.thumbnailator.Thumbnails ;
import net.coobird.thumbnailator.geometry.Positions ;
import sun.misc.BASE64Encoder ;
import com.google.zxing.* ;
import com.google.zxing.common.BitMatrix ;
public class ImgUtils {
public static void main ( String [ ] args ) throws Exception {
}
private static final int BLACK = 0xFF000000 ;
private static final int WHITE = 0xFFFFFFFF ;
private static BufferedImage toBufferedImage ( BitMatrix matrix ) {
int width = matrix . getWidth ( ) ;
int height = matrix . getHeight ( ) ;
BufferedImage image = new BufferedImage ( width , height , BufferedImage . TYPE_INT_RGB ) ;
for ( int x = 0 ; x < width ; x + + ) {
for ( int y = 0 ; y < height ; y + + ) {
image . setRGB ( x , y , matrix . get ( x , y ) ? BLACK : WHITE ) ;
}
}
return image ;
}
private static void writeToFile ( BitMatrix matrix , String format , File file ) throws IOException {
BufferedImage image = toBufferedImage ( matrix ) ;
if ( ! ImageIO . write ( image , format , file ) ) {
throw new IOException ( " Could not write an image of format " + format + " to " + file ) ;
}
}
/**
* 删除二维码白边
* @param matrix
* @return
*/
public static BitMatrix deleteWhite ( BitMatrix matrix ) {
int [ ] rec = matrix . getEnclosingRectangle ( ) ;
int resWidth = rec [ 2 ] + 1 ;
int resHeight = rec [ 3 ] + 1 ;
BitMatrix resMatrix = new BitMatrix ( resWidth , resHeight ) ;
resMatrix . clear ( ) ;
for ( int i = 0 ; i < resWidth ; i + + ) {
for ( int j = 0 ; j < resHeight ; j + + ) {
if ( matrix . get ( i + rec [ 0 ] , j + rec [ 1 ] ) )
resMatrix . set ( i , j ) ;
}
}
return resMatrix ;
}
/**
* 生成一张二维码图片文件
* @param content 二维码内容
* @param widthHeight 宽高度
* @param path 图片存储路径(不带文件名)
* @return
*/
public static String QrImgFile ( String content , int widthHeight , String path ) {
try {
String codeName = UUID . randomUUID ( ) . toString ( ) ; // 二维码的图片名
String imageType = " jpg " ; // 图片类型
MultiFormatWriter multiFormatWriter = new MultiFormatWriter ( ) ;
Map < EncodeHintType , String > hints = new HashMap < EncodeHintType , String > ( ) ;
hints . put ( EncodeHintType . CHARACTER_SET , " UTF-8 " ) ;
BitMatrix bitMatrix = multiFormatWriter . encode ( content , BarcodeFormat . QR_CODE , widthHeight , widthHeight , hints ) ;
bitMatrix = deleteWhite ( bitMatrix ) ;
// BufferedImage imgbuf = toBufferedImage(bitMatrix);
File file1 = new File ( path , codeName + " . " + imageType ) ;
writeToFile ( bitMatrix , imageType , file1 ) ;
return path + codeName + " . " + imageType ;
} catch ( Exception e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return null ;
}
/**
* 生成一张二维码图片(BufferedImage)
* @param content 二维码内容
* @param widthHeight 宽高度
* @return
*/
public static BufferedImage QrImgBuf ( String content , int widthHeight ) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter ( ) ;
Map < EncodeHintType , String > hints = new HashMap < EncodeHintType , String > ( ) ;
hints . put ( EncodeHintType . CHARACTER_SET , " UTF-8 " ) ;
BitMatrix bitMatrix = multiFormatWriter . encode ( content , BarcodeFormat . QR_CODE , widthHeight , widthHeight , hints ) ;
bitMatrix = deleteWhite ( bitMatrix ) ;
BufferedImage imgbuf = toBufferedImage ( bitMatrix ) ;
return imgbuf ;
} catch ( Exception e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return null ;
}
/**
* 生成一张二维码图片(base64)<br>
* data:image/jpg;base64,base64_code
* @param content 二维码内容
* @param widthHeight 宽高度
* @return
*/
public static String QrImgB64 ( String content , int widthHeight ) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter ( ) ;
Map < EncodeHintType , String > hints = new HashMap < EncodeHintType , String > ( ) ;
hints . put ( EncodeHintType . CHARACTER_SET , " UTF-8 " ) ;
BitMatrix bitMatrix = multiFormatWriter . encode ( content , BarcodeFormat . QR_CODE , widthHeight , widthHeight , hints ) ;
bitMatrix = deleteWhite ( bitMatrix ) ;
BufferedImage imgbuf = toBufferedImage ( bitMatrix ) ;
ByteArrayOutputStream os = new ByteArrayOutputStream ( ) ; //新建流。
ImageIO . write ( imgbuf , " jpg " , os ) ; //利用ImageIO类提供的write方法, 将bi以png图片的数据模式写入流。
byte b [ ] = os . toByteArray ( ) ; //从流中获取数据数组。
String str = new BASE64Encoder ( ) . encode ( b ) ;
return str ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return null ;
}
/**
* 图片质量压缩
* @param img 文件流
* @param quality 压缩质量 0~1/1为最高质量
* @return
*/
public static BufferedImage rarImgBuf ( InputStream img , double quality ) {
BufferedImage buf = null ;
try {
buf = Thumbnails . of ( img ) . scale ( 1f ) . outputQuality ( quality ) . asBufferedImage ( ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return buf ;
}
/**
* 图片质量压缩
* @param img 文件地址
* @param quality 压缩质量 0~1/1为最高质量
* @return
*/
public static File rarImgFile ( File img , double quality ) {
File arg0 = img ;
try {
Thumbnails . of ( img ) . scale ( 1f ) . outputQuality ( quality ) . toFile ( arg0 ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return arg0 ;
}
/**
* 裁剪图片<br>
* 从左上角开始裁切<br>
* 另存图片保持等比缩放,若填写的高度不符合自动调整为等比例
*
* @param img 图片文件
* @param p 裁切位置 Positions.TOP/BOTTOM_LEFT/RIGHT、CENTER
* @param x 裁切x轴范围
* @param y 裁切y轴范围
* @param width 另存图片的宽度
* @param height 另存图片的高度
* @return
*/
public static File cutImgFile ( File img , Positions p , int x , int y , int width , int height ) {
File arg0 = img ;
try {
Thumbnails . of ( img ) . sourceRegion ( p , x , y ) . size ( width , height ) . keepAspectRatio ( true ) . toFile ( arg0 ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return arg0 ;
}
/**
* 裁剪图片<br>
* 从左上角开始裁切<br>
* 另存图片保持等比缩放,若填写的高度不符合自动调整为等比例
*
* @param img 图片流
* @param p 裁切位置 Positions.TOP/BOTTOM_LEFT/RIGHT、CENTER
* @param x 裁切x轴范围
* @param y 裁切y轴范围
* @param width 另存图片的宽度
* @param height 另存图片的高度
* @return
*/
public static BufferedImage cutImgBuf ( InputStream img , Positions p , int x , int y , int width , int height ) {
BufferedImage buf = null ;
try {
buf = Thumbnails . of ( img ) . sourceRegion ( p , x , y ) . size ( width , height ) . keepAspectRatio ( true ) . asBufferedImage ( ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return buf ;
}
}