1

in my fragment i can zoom image, now what i want to do is save the image zommed

How can i do this

PJ 1: the image in the top left is the normal image i pick up from ressource, and the second image in the middle is the image i want to associate to the first image.

PJ 2: after the zoom i want save this association in local

 the image in the top left is the normal image i pick up from ressource, and the second image in the middle is the image i want to associate to the first imageafter the zoom  i want save this association in local

Class:

public class MaskFragment extends AbstractFragment{
    @ViewById
    ImageView imageToEdit;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    PointF startPoint = new PointF();
    PointF midPoint = new PointF();
    float oldDist = 1f;
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    @AfterViews
    void initialise() {
    /** * set on touch listner on image */
    imageToEdit.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            System.out.println("matrix=" + savedMatrix.toString());
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                startPoint.set(event.getX(), event.getY());
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(midPoint, event);
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(
                        event.getX() - startPoint.x,
                        event.getY() - startPoint.y
                    );
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        matrix.postScale(
                            scale, scale, midPoint.x,
                            midPoint.y
                        );
                    }
                }
                break;
            }
            view.setImageMatrix(matrix);
            return true;
        }

        @SuppressLint("FloatMath")
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
        }

        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }
    });
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gesture-image="http://schemas.polites.com/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9" >

    <ImageView
        android:id="@+id/imageToEdit"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:scaleType="matrix"
        android:src="@drawable/fake_user" />

    <ImageView
        android:id="@+id/mask"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:layout_centerInParent="true"
        android:src="@drawable/camera_native" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:background="@android:color/black" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Valider"
        android:textColor="@android:color/white"
        android:textSize="25sp" />
</RelativeLayout>

How can i save the second image in the local ?

Thank's for your responses.

arccosinus
  • 41
  • 1
  • 4
tamtoum1987
  • 1,957
  • 3
  • 27
  • 56

2 Answers2

1

Have a look at bitmaps and files in the android api documentation. It will help you a lot to understand how everything works without just copying and pasting.

    //use image from cache
    yourImageView.setDrawingCacheEnabled(true);
    yourImageView.buildDrawingCache(true); //this might hamper performance use hardware acc if available. see: http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean)

    //create the bitmaps
    Bitmap zoomedBitmap = Bitmap.createScaledBitmap(yourImageView.getDrawingCache(true), outputSize, outputSize, true);

    yourImageView.setDrawingCacheEnabled(false);

Now you have your zoomed image and simply need to save it to file. There are much info available on saving to file. In short:

File myFile = new File(thePathOfYourFile); //have a look at the android api docs for File for proper explanation
FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(myFile);
        zoomedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
Chris
  • 4,593
  • 1
  • 33
  • 37
0

Solution of @Sagar Pilkhwal

         public void saveAsJpeg(View view, File file) {
    view.setDrawingCacheEnabled(true);
    Bitmap _b = view.getDrawingCache();
    OutputStream _out = null;
    try {
        _out = new FileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);             
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            _out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
tamtoum1987
  • 1,957
  • 3
  • 27
  • 56