I have a HashMap in which I stored images with Bitmap format. Now the problem is how do I get those images again?
In details,
Here in this hasmap, I stored my Bitmap image as an object.
ArrayList<HashMap<String,Object>> mImgList = new ArrayList<HashMap<String,Object>>();
Bitmap img = imgLoader.decodeSampledBitmapFactoryFromUrl(x,x,x,x);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TAG_IMG, img);
map.put(TAG_NAME, name);
map.put(TAG_ID, cont_id);
mImgList.add(map);
I use this hashmap in the listview to diplay img and text, everything's fine.
But now I need to display some of those images in a ImageView, but I don't know how to get the image as those Bitmap in the HashMap is becoming an object.
I tried to use:
Bitmap bmp = mImgList.get(x).get(TAG_IMG);
but what i got is actualy an object not a bitmap. so how should i get my bitmap? or are there any other way I can display this picture on the alertdialog? thanks!
Turns out no one answered my question...But I end up converting bitmap into drawable so that I can save them into hashmap and get them when i want.
Bitmap bitmap = (Bitmap) mMap.get("bitmap_key_001");
Related
I have created a bitmap that forms a linear gradient background. I am trying to save it into the device's storage but the saved image has very low quality.
Code to save the bitmap image:
public void saveImage(Bitmap bitmap) {
String filename = "dy/dx_wallpaper-" + System.currentTimeMillis();
String url = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, filename, "Gradient Wallpaper");
Toast.makeText(MainActivity.this, "Saved as " + url, Toast.LENGTH_SHORT).show();
}
Image should be like this
But it's like this
Am I doing something wrong?
Thanks in advance folks! :)
It seems the insertImage method only stores a thumbnail of the original bitmap. Here is a possible solution which may help:
How to insert high quality Bitmap image in android into Gallery
I want to create a chart with Object Refinery JFreeChart. After that, I want to create a pdf with Apache PDFBox, where I use the chart from JFreeChart. When I save the chart as png or jpg, then the quality of the chart is very bad. So I save the chart as svg and convert it with Apache Batik to pdf. The quality in the pdf is good but I can't alter the position of the chart in the pdf.
How can I alter the position of the chart?
Is there a better solution to generate a chart as svg and put it in a pdf?
Here is my code of how I convert the svg to pdf:
public void convertSVGtoPDF() throws TranscoderException, IOException{
//Step -1: We read the input SVG document into Transcoder Input
String svg_URI_input =
Paths.get("output_pie_chart.svg").toUri().toURL().toString();
System.out.println("Path="+svg_URI_input);
TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);
//Step-2: Define OutputStream to PDF file and attach to TranscoderOutput
OutputStream pdf_ostream = new FileOutputStream("FinalPDF.pdf");
TranscoderOutput output_pdf_file = new TranscoderOutput(pdf_ostream);
// Step-3: Create a PDF Transcoder and define hints
Transcoder transcoder = new PDFTranscoder();
transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new
Float(800)); transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(800));
transcoder.addTranscodingHint(PrintTranscoder.KEY_MARGIN_TOP,new Float (80));
// Step-4: Write output to PDF format
transcoder.transcode(input_svg_image, output_pdf_file);
// Step 5- close / flush Output Stream
pdf_ostream.flush();
pdf_ostream.close();
}
}
My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?
I tried SetAdjustViewBounds() but it didn't work for me.
Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.
imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;
Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout
//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
Finally I found a way how to measure my ImageView and here I will post my answer.
I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:
How to get the width and height of an android.widget.ImageView?
Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.
Implementing the View.IOnTouchListener interface
Here is how I combined things. First I created class that will do the measure.
public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
ImageView imageView;
public MyPredrawListener(ImageView img)
{
imageView = img;
}
public bool OnPreDraw()
{
imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
int finalHeight = imageView.MeasuredHeight;
int finalWidth = imageView.MeasuredWidth;
Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
imageView.SetImageBitmap(bitmap);
//Test to see result
Canvas cnv = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color = new Color(255, 255, 0);
cnv.DrawColor(new Color(128, 128, 128));
cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);
return true;
}
}
And in code where I create my imageView I set the listener like this.
imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);
In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.
Problem:
I have used following code for scaling image and make it fit to bounds of UITableviewCell in monotouch.This code is scaling image but it is making image color faint means it is not maintaining color combination.So please provide any better solution.
Code:
public static UIImage Scale (UIImage source, SizeF newSize)
{
UIGraphics.BeginImageContext (newSize);
var context = UIGraphics.GetCurrentContext ();
context.TranslateCTM (0, newSize.Height);
context.ScaleCTM (1f, -1f);
context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), source.CGImage);
var scaledImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return scaledImage;
}
If you want scale UIImage, then see this method. It works fine. You should cache method's result in file system to avoid image scaling on every GetCell.
If you source images are the same or almost the same size like UIImageViews in UITableViewCell Mohib Sheth solution with setting ContentMode property is best. It source image have much greater size than UIImageViews in UITableViewCell, scrolling speed will be lower.
Read all about ContentMode for UIView in iOS here
For your requirement you would need to set the ContentMode property to UIViewContentModeScaleAspectFit
I cant help you with a sample code, since you haven't provided any code on how you are creating & populating the TableView.
I have a 16x16 bitmap and want to create an SVG that contains 16x16 squares with the colors of the pixels of the image. Is there an easy way to achieve this?
My current thoughts go into the direction of using Python and PIL to read the bitmap image and dynamically create an SVG image file with the corresponding objects. But this feels a little clumsy and like reinventing the wheel.
Is there a better way to do this?
If you don't need the output to be SVG, I would suggest using an HTML5 Canvas where you can sample the pixels of the image client-side (using getImageData() on the context) and then draw your own up-scaled image. Or, if you need SVG, you could still use Canvas for the image sampling and then use procedurally-created <rect/> elements in SVG for each pixel.
I've written an example using just HTML Canvas so you can see how to do this. In short:
function drawPixelated(img,context,zoom,x,y){
if (!zoom) zoom=4; if (!x) x=0; if (!y) y=0;
if (!img.id) img.id = "__img"+(drawPixelated.lastImageId++);
var idata = drawPixelated.idataById[img.id];
if (!idata){
var ctx = document.createElement('canvas').getContext('2d');
ctx.width = img.width;
ctx.height = img.height;
ctx.drawImage(img,0,0);
idata = drawPixelated.idataById[img.id] = ctx.getImageData(0,0,img.width,img.height).data;
}
for (var x2=0;x2<img.width;++x2){
for (var y2=0;y2<img.height;++y2){
var i=(y2*img.width+x2)*4;
var r=idata[i ];
var g=idata[i+1];
var b=idata[i+2];
var a=idata[i+3];
context.fillStyle = "rgba("+r+","+g+","+b+","+(a/255)+")";
context.fillRect(x+x2*zoom, y+y2*zoom, zoom, zoom);
}
}
};
drawPixelated.idataById={};
drawPixelated.lastImageId=0;
If you really need SVG involved, I'd be happy to write an example that dynamically generated that.
Edit: OK, I've created an SVG version just for fun and practice. :)
As an aside (from an initial misreading of your question) this demo file from ASVG3 their old SVG Examples Page shows how to use some complex compositing of many different effects to create pixelation on arbitrary vector data. Unfortunately the demo does not load in Chrome, having been hardwired to require the (now-discontinued) Adobe SVG Viewer.