Adding Text to a Picture using a Camera Application (Android)

If you want to add text to a picture that you took from your Android camera app that you built then you came to the right spot. This is an addition to my post Camera Application in Android Example.  So if you do not have a working camera app yet then I would recommend using the code posted there.  It is only small addition and you should not have an issue adding the code to your existing code.  Here is an example of a picture with text printed on the picture:

Camera app with text overlay

addText() 

This method takes a Bitmap and outputs a Bitmap with the text printed on it.

private Bitmap addText(Bitmap toEdit){

		Bitmap dest = toEdit.copy(Bitmap.Config.ARGB_8888, true);		
	        Canvas canvas = new Canvas(dest);

		    Paint paint = new Paint();  //set the look
		    paint.setAntiAlias(true);
		    paint.setColor(Color.GREEN);
		    paint.setStyle(Style.FILL);
		    paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);

    	int pictureHeight = dest.getHeight();
    	paint.setTextSize(pictureHeight * .04629f);

    	canvas.drawText("Hello World" , dest.getWidth()/2,  100, paint);
	return dest;
	}

 

PictureCallback

There is probably more efficiency way of doing this but it works. I feel like I am creating a file twice.

private PictureCallback mPicture = new PictureCallback() {

	    @Override
	    public void onPictureTaken(byte[] data, Camera camera) {
	        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

	        if (pictureFile == null){
	            Log.d("ERROR", "Error creating media file, check storage permissions:" );
	        }

	        try {
	            FileOutputStream fos = new FileOutputStream(pictureFile);
	            fos.write(data);

	        } catch (FileNotFoundException e) {
	            Log.d("ERROR", "File not found: " + e.getMessage());
	        } catch (IOException e) {
	            Log.d("ERROR", "Error accessing file: " + e.getMessage());
	        }

			Bitmap myBitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
			Bitmap second = addText(myBitmap);

			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			second.compress(CompressFormat.JPEG, 100, bos);

			byte[] bitmapdata = bos.toByteArray();

			//write the bytes in file
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(pictureFile);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fos.write(bitmapdata);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        setImage();

	        mCamera.startPreview();
	    }
	};

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys