Java example _java to crawl and generate thumbnails from URLs

Source: Internet
Author: User

Java crawls and generates thumbnails based on URLs

Copy Code code as follows:

public static Bitmap loadimagefromurl (String url, int sc) {
URL m;
InputStream i = null;
Bufferedinputstream bis = null;
Bytearrayoutputstream out = null;
byte isbuffer[] = new byte[1024];
if (url = null)
return null;
try {
m = new URL (URL);
i = (InputStream) m.getcontent ();

bis = new Bufferedinputstream (i, 1024 * 4);
out = new Bytearrayoutputstream ();
int len = 0;
while (len = Bis.read (isbuffer))!=-1) {
Out.write (isbuffer, 0, Len);
}
Out.close ();
Bis.close ();
catch (Malformedurlexception E1) {
E1.printstacktrace ();
return null;
catch (IOException e) {
E.printstacktrace ();
}
if (out = = null)
return null;
byte[] data = Out.tobytearray ();
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.injustdecodebounds = true;
Bitmapfactory.decodebytearray (data, 0, data.length, options);
Options.injustdecodebounds = false;
int be = (int) (Options.outheight/(float) SC);
if (be <= 0) {
be = 1;
else if (be > 3) {
be = 3;
}
Options.insamplesize = be;
Bitmap bmp = null;
try {
BMP = Bitmapfactory.decodebytearray (data, 0, data.length, options); return thumbnail image
catch (OutOfMemoryError e) {
Todo:handle exception
System.GC ();
BMP = NULL;
}
return BMP;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.