I’ve been trying to improve this simple app which adds a text (“hola”) to a picture taken by the Android Camera App and the saves the image to the sd card.
However, i have only managed to add the text to the thumbnail image from the returned data, and save the file to sd card.
Could anyone point me in the right direction to do the exact same thing but with the full-size photo?
Thanks a lot!!
What I have so far: (using code from tutorials and answers to similar questions on stackoverflow)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button captureBtn = (Button)findViewById(R.id.capture_btn);
captureBtn.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
Intent tomaFotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(tomaFotoIntent, CAMERA_CAPTURE);
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Device doesn't support capturing images";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//user is returning from capturing an image using the camera
if(requestCode == CAMERA_CAPTURE){
//get the Uri for the captured image
picUri = data.getData();
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//agregamos texto
bmConTexto = writeTextOnDrawable(thePic, "hola");
//guardamos la nueva imagen
saveBitmap(bmConTexto.getBitmap());
//retrieve a reference to the ImageView
ImageView picView = (ImageView)findViewById(R.id.picture);
//display the returned cropped image
picView.setImageBitmap(bmConTexto.getBitmap());
}
}
}
private BitmapDrawable writeTextOnDrawable(Bitmap thePic, String text) {
Bitmap bm = thePic.copy(Bitmap.Config.ARGB_8888, true);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(20);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//Calculate the positions
int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(text, xPos, yPos, paint);
return new BitmapDrawable(getResources(), bm);
}
public void saveBitmap(Bitmap bm)
{
try
{
String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
String mFilePath = mBaseFolderPath + "abcd.jpg";
FileOutputStream stream = new FileOutputStream(mFilePath);
bm.compress(CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
}
catch(Exception e)
{
Log.e("Could not save", e.toString());
}
}
It sounds like you are trying to take a picture and write “Hola” on it, if this is the case have you tried looking in to
takePicture()of the android camera api? If not can you elaborate the question?Edit: pasted from the top of the page of the link above: