zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java根据url抓取并生成缩略图的示例

JAVA 示例 生成 url 根据 抓取 缩略图
2023-06-13 09:15:26 时间

java根据url抓取并生成缩略图

复制代码代码如下:

publicstaticBitmaploadImageFromUrl(Stringurl,intsc){
       URLm;
       InputStreami=null;
       BufferedInputStreambis=null;
       ByteArrayOutputStreamout=null;
       byteisBuffer[]=newbyte[1024];
       if(url==null)
           returnnull;
       try{
           m=newURL(url);
           i=(InputStream)m.getContent();

           bis=newBufferedInputStream(i,1024*4);
           out=newByteArrayOutputStream();
           intlen=0;
           while((len=bis.read(isBuffer))!=-1){
               out.write(isBuffer,0,len);
           }
           out.close();
           bis.close();
       }catch(MalformedURLExceptione1){
           e1.printStackTrace();
           returnnull;
       }catch(IOExceptione){
           e.printStackTrace();
       }
       if(out==null)
           returnnull;
       byte[]data=out.toByteArray();
       BitmapFactory.Optionsoptions=newBitmapFactory.Options();
       options.inJustDecodeBounds=true;
       BitmapFactory.decodeByteArray(data,0,data.length,options);
       options.inJustDecodeBounds=false;
       intbe=(int)(options.outHeight/(float)sc);
       if(be<=0){
           be=1;
       }elseif(be>3){
           be=3;
       }
       options.inSampleSize=be;
       Bitmapbmp=null;
       try{
           bmp=BitmapFactory.decodeByteArray(data,0,data.length,options);//返回缩略图
       }catch(OutOfMemoryErrore){
           //TODO:handleexception
           System.gc();
           bmp=null;
       }
       returnbmp;
   }