//defaultSuffix是jpg
創(chuàng)新互聯(lián)是一家專業(yè)提供榆陽企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、html5、小程序制作等業(yè)務(wù)。10年已為榆陽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
public static final boolean resizeImage(String fileName, String suffix) throws Exception {
boolean uploaded = false;
BufferedImage input;
if(suffix.equalsIgnoreCase("tif")||suffix.equalsIgnoreCase("tiff")||suffix.equalsIgnoreCase("png")) {
RenderedImage image = JAI.create("fileload", TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix));
WritableRaster raster = image.copyData(null);
BufferedImage bi = new BufferedImage( image.getColorModel(), raster, true, null);
BufferedImage bi2 = new BufferedImage( maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setBackground(java.awt.Color.WHITE);//把tiff和png轉(zhuǎn)換后的背景設(shè)置為白色
g2.fillRect(0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth());
g2.drawImage(bi, 0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), null);
PlanarImage pi = PlanarImage.wrapRenderedImage(bi2);
JAI.create("FileStore", pi, TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix).replaceAll("\\\\","\\\\\\\\"), "JPEG", new JPEGEncodeParam());
input = pi.getAsBufferedImage();
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
}
else {
input = ImageIO.read(new File(TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix)));
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
BufferedImage output2 = new BufferedImage(maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2 = output2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(input,0,0,maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), null);
ImageIO.write(output2, defaultSuffix, new File(TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix)));
}
uploaded = true;
return uploaded;
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test{
public static void main(String[] args){
try {
File f1 = new File("a.jpg");
BufferedImage image = ImageIO.read(f1);
Graphics g = image.getGraphics();
g.setFont(new Font("Serif",Font.BOLD,20));
g.setColor(Color.red);
g.drawString("happy new year!", 10, 20);
File f2 = new File("copy.jpg");
ImageIO.write(image, "JPEG", f2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
final ImageView iv=(ImageView)findViewById(R.id.iv);
Button bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
// TODO: Implement this method
if(iv.getDrawable()!=null)
iv.setImageResource(R.id.photo);
else iv.setImageResource(0);
}
});
這樣可以
public?static?void?main(String[]?args)?{
String[]?temps?=?{"A","B","C","D","E","F","G"};
for?(int?i?=?1;?i?=?temps.length;?i++)?{
for(int?j?=?temps.length;?j??i;?j--){???
System.out.print("?");???
}
for?(int?j?=?0;?j??i*2-1;?j++)?{
??System.out.print(temps[i-1]);
}
System.out.println();
}
}
運(yùn)行結(jié)果
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
FFFFFFFFFFF
GGGGGGGGGGGGG
輸出圖片的base64編碼
//imgFile是圖片的路徑
public?static?void?getImageStr(String?imgFile)?{
InputStream?inputStream?=?null;
byte[]?data?=?null;????
try?{
inputStream?=?new?FileInputStream(imgFile);
data?=?new?byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}????//?加密
BASE64Encoder?encoder?=?new?BASE64Encoder();????
System.out.println(encoder.encode(data));
}
1、代碼如下:
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World!");
//主循環(huán)
? for(int i =10;i0;i--){
? ? ? //輸出空格
? ? ? for(int k=i;k0;k--){System.out.print(" ");}
//輸出數(shù)字
? ? ? for(int j=i;j=10;j++){
? ? ? ? ? System.out.print(j+" ");? ? ? ? ? ? ?
? ? ? }System.out.println(" ");
? }
}
}
2、效果如圖