Copy a specified area from a bitmap

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Copy a specified area from a bitmap

Postby shyjumon.n » Sat Apr 25, 2009 1:00 pm

Hi All,
I have few small questions related to canvas and bitmap APIs.

I have a image file (R.drawable.img1 or "/data/img1.jpg"), i am trying to copy the specified areas of the image to different files. For example: the specified point is (x1, y1) then i am trying to have a rectange with left=x1-20, top=y1-20, right=x1+20, bottom=y1+20 and this rectangle to a file ("/data/1.jpg").

Is there any way to do it. Please help me with some links. I tried in the following way:
1. create a canvas and
Canvas c = new Canvas();
c.setBitmap(Img1);
2. draw a circle around the specified point and by using picture API (record) and save to a file.
But this seems not giving me proper results.

In short i wanted get a specified rectangle to a file. Please advice me.

Also another question is :
I have a custom Imageview (say class Test), this will be able to display an image on screen and also able draw something on the image. Now i have another class which has a Layout (i am calling setContentView(R.layout.main) form the class.
I want the custom view output (the view it deploy to the screen when i call setContentView(new Test(this))) to the Imageview which i have in my main.xml.

Is the above is possible.

thank you for any help or links.

SMN
shyjumon.n
Freshman
Freshman
 
Posts: 9
Joined: Wed Mar 18, 2009 8:13 am

Top

Re: Copy a specified area from a bitmap

Postby lefish » Wed May 06, 2009 8:06 am

I think you can call compress method in Bitmap. here is code snippets:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.FileOutputStream;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.graphics.Bitmap;
  8.  
  9. import android.graphics.BitmapFactory;
  10.  
  11. import android.os.Bundle;
  12.  
  13.  
  14.  
  15. public class HActivity extends Activity {
  16.  
  17.  
  18.  
  19.         @Override
  20.  
  21.         public void onCreate(Bundle savedInstanceState) {
  22.  
  23.                 super.onCreate(savedInstanceState);
  24.  
  25.                 setContentView(R.layout.main);
  26.  
  27.  
  28.  
  29.                 Bitmap source = BitmapFactory.decodeResource(getResources(),
  30.  
  31.                                 R.drawable.icon);
  32.  
  33.                 Bitmap dest = Bitmap.createBitmap(source, 0, 0, source.getWidth() / 2,
  34.  
  35.                                 source.getHeight() / 2);
  36.  
  37.  
  38.  
  39.                 try {
  40.  
  41.                         FileOutputStream fout = new FileOutputStream("/sdcard/cuteIcon.png");
  42.  
  43.                         dest.compress(Bitmap.CompressFormat.PNG, 100, fout);
  44.  
  45.                         fout.close();
  46.  
  47.                 } catch (Exception e) {
  48.  
  49.                         e.printStackTrace();
  50.  
  51.                 }
  52.  
  53.  
  54.  
  55.         }
  56.  
  57.  
  58.  
  59. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
lefish
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 10, 2009 4:47 am

Re: Copy a specified area from a bitmap

Postby Manjusha » Fri Jul 23, 2010 8:37 am

Actually i m also having some problems in compress methods of bitmap.
I have tried above suggested code,code is running but i m nt able to see any cuteIcon.png in sdcard....
Will u plz tell what can be the reason?
Manjusha
Freshman
Freshman
 
Posts: 2
Joined: Fri Jul 23, 2010 8:27 am

Top

Return to General

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest