uso por debajo de código para lograr su requerimiento
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage); // the original file yourimage.jpg i added in resources
Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
String yourText = "My custom Text adding to Image";
Canvas cs = new Canvas(dest);
Paint tPaint = new Paint();
tPaint.setTextSize(35);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Style.FILL);
cs.drawBitmap(src, 0f, 0f, null);
float height = tPaint.measureText("yY");
float width = tPaint.measureText(yourText);
float x_coord = (src.getWidth() - width)/2;
cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can
try {
dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ImageAfterAddingText.jpg")));
// dest is Bitmap, if you want to preview the final image, you can display it on screen also before saving
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Tienes que utilizar a continuación el permiso en el archivo de manifiesto.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
para mi dispositivo es el camino /sdcard
acceder a la tarjeta SD externa, puede variar para otros dispositivos. Algunos dispositivos pueden tener /mnt/sdcard
, ya sea para tarjetas sd internas. Simplemente verifíquelo antes de usar este código.
Realmente escribí el código anterior para alguna otra pregunta, que requería marca de tiempo en la foto después de capturado desde la cámara. Te di la misma solución con algunas modificaciones para tus requisitos específicos.
Espero que puedan entender esto. Si tiene alguna duda sobre el código, no dude en preguntar.
significa que desea añadir texto en un archivo de imagen? –
Tengo una imagen (.jpg) y quiero agregar el texto del usuario en la parte superior central y generar un nuevo jpg con el texto incluido – Addev
verifique mi respuesta. –