saving images with text

Problems with Canvas, OpenGL, etc...

saving images with text

Postby rjuy » Thu Apr 28, 2011 8:29 am

Hello,

I'm new to saving images in Android,

Can someone help me with this.

Currently I have a school project for my removal and this project has a working code to have a screen with an image and a text on the center. Now what I want is to save this image together with the text. But what was being saved is the image only, the text was not included.

PLease see code below

Code: Select all
package com.wglxy.android;

import java.io.*;
import android.content.Context;
import android.graphics.*;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class SaveImage extends GraphicsActivity {
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this)); 
    }
   
    public static class SampleView extends View {
        private static final int WIDTH = 5;
        private static final int HEIGHT = 5;
        private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);
        private final Bitmap mBitmap;
        private final float[] mVerts = new float[COUNT*2];
        private final float[] mOrig = new float[COUNT*2];
        private int imgCtr = 0;
        private Paint p = new Paint();
        private Path    path;
       
        private static void setXY(float[] array, int index, float x, float y) {
            array[index*2 + 0] = x;
            array[index*2 + 1] = y;
        }

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
           
            mBitmap = BitmapFactory.decodeResource(getResources(),
                                                     R.drawable.bcimage1);
      
            float w = mBitmap.getWidth();
            float h = mBitmap.getHeight();
           
            // construct our mesh
            int index = 0;
            for (int y = 0; y <= HEIGHT; y++) {
                float fy = h * y / HEIGHT;
                for (int x = 0; x <= WIDTH; x++) {
                    float fx = w * x / WIDTH;                   
                    setXY(mVerts, index, fx, fy);
                    setXY(mOrig, index, fx, fy);
                    index += 1;
                }
            }
        }
       
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);
            canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                                  null, 0, p);
            canvas.drawText("Practice text here", 100, 100, p);
           
            File myDir=new File("/sdcard/saved_images");
            myDir.mkdirs();
            imgCtr++;
            String fname = "image-" + imgCtr + ".jpg";
            File file = new File (myDir, fname);
            if (file.exists ()) file.delete ();
            saveAsJpg (file);
        }
       
        public void saveAsJpg (File f){
           String fname = f.getAbsolutePath ();
          FileOutputStream fos = null;
          try {
              fos = new FileOutputStream (f);
              mBitmap.compress (CompressFormat.JPEG, 95, fos);
              Toast.makeText(getContext(),"Saved " + fname, Toast.LENGTH_LONG).show ();
          } catch (Throwable ex) {
             Toast.makeText (getContext(), "Error: " + ex.getMessage (), Toast.LENGTH_LONG).show ();             
             ex.printStackTrace ();
          }
           
      } // end saveAsJpg
    }
}


Please help!

thanks

rjuy
rjuy
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Mon Oct 25, 2010 2:38 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests