PCB rescaling all pictos greater than 100dx width

parent 741bbc42
...@@ -78,7 +78,7 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -78,7 +78,7 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
File file=new File(img.get_url()); File file=new File(img.get_url());
is=new FileInputStream(file); is=new FileInputStream(file);
} }
int size=img.save_bitmap(this.context, is,this.source==source.local); int size=img.save_bitmap(this.context, is);
allsize+=size; allsize+=size;
i++; i++;
} catch (IOException e) { } catch (IOException e) {
......
...@@ -124,22 +124,26 @@ public class Img { ...@@ -124,22 +124,26 @@ public class Img {
* @param is the stream where the image is available * @param is the stream where the image is available
* @throws IOException * @throws IOException
*/ */
public int save_bitmap(Context context, InputStream is,boolean resize) throws IOException { public int save_bitmap(Context context, InputStream is) throws IOException {
final float MAX_WIDTH=100;
File file = file(context); File file = file(context);
FileOutputStream os = new FileOutputStream(file); FileOutputStream os = new FileOutputStream(file);
try { try {
this.bitmap=BitmapFactory.decodeStream(is); this.bitmap=BitmapFactory.decodeStream(is);
if (resize) this.bitmap=new ImgTools(this.bitmap).resize(78,66); if (this.bitmap.getWidth()>MAX_WIDTH) {
this.bitmap = new ImgTools(this.bitmap).rescale(MAX_WIDTH / (float) this.bitmap.getWidth());
}
}catch(java.lang.OutOfMemoryError err) { }catch(java.lang.OutOfMemoryError err) {
Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url()); Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url());
} }
ByteArrayOutputStream outstream = new ByteArrayOutputStream(); ByteArrayOutputStream outstream = new ByteArrayOutputStream();
this.bitmap.setHasAlpha(true); this.bitmap.setHasAlpha(true);
this.bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream); this.bitmap.compress(Bitmap.CompressFormat.PNG, 50, outstream);
byte[] byteArray = outstream.toByteArray(); byte[] byteArray = outstream.toByteArray();
os.write(byteArray); os.write(byteArray);
outstream.close(); outstream.close();
os.close();
System.gc();
return byteArray.length; return byteArray.length;
} }
......
...@@ -39,4 +39,11 @@ public class ImgTools { ...@@ -39,4 +39,11 @@ public class ImgTools {
// objeto drawable y así asignarlo a un botón, imageview... // objeto drawable y así asignarlo a un botón, imageview...
return resizedBitmap; return resizedBitmap;
} }
public Bitmap rescale(float scale) {
int width = (int) (scale * (float) this.bitmap.getWidth());
int height = (int) (scale * (float) this.bitmap.getHeight());
return resize(width,height);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment