Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

Wednesday, June 12, 2013

Sending BitMap Between Activities:

Sending BitMap Between Activities:

In Activity 1 :
do like this---------------->
Which image you want to send to next activity
bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); // here imageView is an XML related image view.
       
Intent intent = new Intent(mContext, NextActivity.class);
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
        intent.putExtra("byteArray", bs.toByteArray());
        startActivity(intent);


In Activity 2 :
try{
        if(getIntent().hasExtra("byteArray")) {
        bitmap = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);      
                imageView.setImageBitmap(bitmap); // here imageView Activity 2 related imageView
            }
        }catch (Exception e) {
e.printStackTrace();
}

Sunday, May 13, 2012

Android - Function to convert ImageURL to Bitmap object


Android - Function to convert ImageURL to Bitmap object





IMAGE URL TO BITMAP

Use this function “convertURLtoBitmap”

public Bitmap convertURLtoBitmap(String src) {

                try {

                                URL url = new URL(src);
                                                
                                HttpURLConnection connection = (HttpURLConnection) url
                                                .openConnection();

                                connection.setDoInput(true);
                                connection.connect();

                                InputStream input = connection.getInputStream();
                                Bitmap myBitmap = BitmapFactory.decodeStream(input);

                                return myBitmap;

                }

                catch (IOException e) {

                                e.printStackTrace();
                                return null;

                }
}

Monday, April 30, 2012

Crop an Image in ANDROID.


Crop an Image in ANDROID.




Cropping an image in ANDROID programmatically has always been a problem for ANDROID developers.
So Here I am putting a sample code to demonstrate this.
For cropping an image in ANDROID we need a source bitmap which is our image itself, the other one is the target bitmap which we have to
set the size as we want and a matrix.
Below code is a fully working code, just copy and paste this code to your new project.
The XML for the code is also provided below.
Make sure that you have an image named “image.png” in your resources.
Assuming the image to be of size 300×300 pixels.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package Move3.pack;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.FrameLayout;
 
@SuppressWarnings({ "deprecation", "unused" })
public class Move extends Activity {
 
    public FrameLayout board;
    public View part1;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         board = new FrameLayout(this);
         board = (FrameLayout)findViewById(R.id.Board);
         part1 = new View(this);
         part1 = findViewById(R.id.part1);
        try{
 
            Paint paint = new Paint();
            paint.setFilterBitmap(true);
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.image);
 
            int targetWidth  = 300;
            int targetHeight = 300;
 
            Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
 
            RectF rectf = new RectF(0, 0, 100, 100);
 
            Canvas canvas = new Canvas(targetBitmap);
            Path path = new Path();
 
            path.addRect(rectf, Path.Direction.CW);
            canvas.clipPath(path);
 
            canvas.drawBitmap( bitmapOrg, new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight()),
                            new Rect(0, 0, targetWidth, targetHeight), paint);
 
            Matrix matrix = new Matrix();
            matrix.postScale(1f, 1f);
            Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 100, 100, matrix, true);
 
            /*convert Bitmap to resource */
            BitmapDrawable bd = new BitmapDrawable(resizedBitmap);
 
            part1.setBackgroundDrawable(bd);
 
        }
        catch(Exception e){
            System.out.println("Error1 : " + e.getMessage() + e.toString());
      }
 
    }
main.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/Board"
     android:layout_gravity="top">
 
<View
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:id="@+id/part1"
 
        >
</FrameLayout>
crop image in android Example
cropping image in android