how to add text to save image by camera in android app - android

Im using camera in my android app but I have to add text to save captured image (after capturing image app should ask to add text to save captured image). How can I do this ?

What I think you are trying to do is to save a note with the image.
You should create a directory in which you will save the image with say name "image****.jpg". In the same directory create a file with same name but with different extension on no extension at all. (like "image****.txt").
When you access the image file, open the note file seperately and process it.

Try this to write text to bitmap from camera...
private Bitmap writeTextBitmap(Bitmap bitmap,String text) {
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(bitmap);
//If the text is bigger than the canvas , reduce the font size
if(textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
paint.setTextSize(convertToPixels(mContext, 7)); //Scaling needs to be used for different dpi's
//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 bitmap;
}

Related

Drawing Canvas with fixed scale

I have a button which is drawn using canvas onDraw method. The button itself may will change size depending on device, but I want the text I'm drawing inside to remain consistent, even with different screen size or font size user may have set in settings. That is why I'm doing it in pixels.
#Override
public void onDraw(Canvas canvas) {
// 1. draw background
paint.setStyle(Paint.Style.FILL);
paint.setColor(colorBg);
canvas.drawRect(padding, padding, w-padding, h-padding, paint);
// 2. draw border
paint.setStyle(Paint.Style.STROKE);
paint.setColor(colorBorder);
canvas.drawRect(padding, padding, w-padding, h-padding, paint);
// 3. draw text
int fontSizeTextNew = canvas.getHeight() / 100 * 19;
int fontSizePaddingNew = canvas.getHeight() / 100 * 19;
textPaint.setTextSize(fontSizeTextNew);
textPaint.setShader(blackShader);
textPaint.setTypeface(Typeface.DEFAULT);
textPaint.getTextBounds(
prefTitle, // text
0, // start
prefTitle.length(), // end
rectangle // bounds
);
canvas.drawText(
prefTitle,
canvas.getWidth()/2,
Math.abs(rectangle.height()) + padding + fontSizePaddingNew,
textPaint
);
// 4. draw icon
int fontSizeIconNew = canvas.getHeight() / 100 * 55;
String str = prefIcon;
textPaint.setTypeface(awesomeFont);
textPaint.setTextSize(fontSizeIconNew);
textPaint.setShader(yellowShader);
// center for the text
// https://stackoverflow.com/questions/11120392/android-center-text-on-canvas
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
canvas.drawText(
str,
canvas.getWidth()/2,
Math.abs(yPos) + fontSizeIconPadding,
textPaint
);
}
This 19% looks nice right now but it will look different on variouse screens. What is your approach to this problem? how you would solvethis problem?
Button1 This is how it may look

Draw text on canvas dont work with mutable bitmap

I want to use custom markers on google maps in my Android app.
For this I have a method, which creates me Bitmaps. Each Bitmap is a marker including special text on it.
I have done this and it works ok on my 2.3.3 Android. But on other devices it crashes, cause I dont use mutable Bitmaps.
I changed my code to mutable Bitmaps, but now the text isn't visible, just the marker bitmap without text.
My method:
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.marker);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(convertToPixels(context, 8));
Rect textRect = new Rect();
paint.getTextBounds(markerText, 0, markerText.length(), textRect);
// THIS LINE IS NEW FOR MUTABLE
Bitmap mutableBitmap = bm.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
if(textRect.width() >= (canvas.getWidth() - 4)){
paint.setTextSize(convertToPixels(context, 7));
}
int xPos = (canvas.getWidth() / 2) - 2;
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(markerText, xPos, yPos, paint);
return bm;
What I am doing wrong? I just add the line with mutable.
best regards
Your method returns the immutable bitmap that you obtained using BitmapFactory.decodeResource().
You must return the mutable bitmap you drawed on:
return mutableBitmap;

How to write text on a huge bitmap

I'm writing a simple camera app, that would let the user take a picture, and the app would write some information into the image.
I'm getting out of memory errors, because the image could be huge. Depends on the camera hardware.
This is how I'm writing the text onto the image:
public static byte[] writeTextOnImage(byte[] imgData, String text) {
Bitmap bitmap = BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
int dist = 30;
Rect areaRect = new Rect(dist, mutableBitmap.getHeight() - dist, 170, (int) (mutableBitmap.getHeight() - dist * 1.6f));
paint.setColor(Color.BLACK);
canvas.drawRect(areaRect, paint);
RectF bounds = new RectF(areaRect);
bounds.right = paint.measureText(text, 0, text.length());
bounds.bottom = paint.descent() - paint.ascent();
bounds.left += (areaRect.width() - bounds.right) / 2.0f;
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f;
paint.setColor(Color.WHITE);
canvas.drawText(text, bounds.left, bounds.top - paint.ascent(), paint);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
I guess I should not even think about loading the whole image, but I still have to write on it somehow. Is it possible to edit an image file without loading all of it into the memory?
You can manipulate image in NDK. Memory available to native code is limited only by device RAM. Downside is that you have to decode image in native code.

Canvas drawtext positioning

I am creating a drawing tool, where user add text to image. While making the text draw to bitmap via canvas position is not being set properly.
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTypeface(tf);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(30);
int xPos = layoutTextViewContainer.getLeft();
int yPos = layoutTextViewContainer.getTop();
canvas.drawText(text, xPos, yPos, paint);
Paint
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
textRect.offset(0, -textRect.top);
Canvas canvas = new Canvas(bm);
layoutTextViewContainer holds the edit text.
Screen shot for more clarification.
Black text is written and Red text is preview of embed in image
Got the solution. the values should be pixel independent
convert xPos and yPos as below before passing to drawText
xPos = (int) (xPos / getResources().getDisplayMetrics().density);
yPos = (int) (yPos / getResources().getDisplayMetrics().density);
Its even better to use Drawing cache and save that to any file location.
In this case we dont have to much bother about positioning.
Wrap all in one layout and get the
view.getDrawingCache()

Not getting complete text while Animation on Canvas : Android,

I want to get an animated text in Android where it should run from left to right on the screen.
Eg.
I want to get this text
private static final String QUOTE =
"Nobody uses Java anymore. It's this big heavyweight ball and chain.";
running from right to left using animation.
However, this text gets truncated to "Nobody uses Java anymore. It's" in Portrait mode and to
"Nobody uses Java anymore. It's this big heavyweight" in landscape mode.
This is the code that I have used
Paint paint = new Paint();
paint.setColor(Color.BLUE);
private static final String QUOTE =
"Nobody uses Java anymore. It's this big heavyweight ball and chain.";
paint.setTextSize(20);
paint.setAntiAlias(true);
int len= QUOTE.length();
canvas.scale(1,-1);
canvas.drawText(QUOTE, 0, len-1, 100, 60, paint);
canvas.drawText(QUOTE,0,100 , paint);
createAnim(canvas);
private void createAnim(Canvas canvas) {
anim2= new TranslateAnimation(500,-500,30,30);
anim2.setRepeatCount(Animation.INFINITE);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());
anim2.setRepeatMode(Animation.RESTART);
anim2.setDuration(1000L);
startAnimation(anim2);
}
How to do Text Animations using Android SDK?
Please refer this post for text animations.
Please check height and width of canvas ,
// Custom Font Text
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(),
(int) getWindowManager().getDefaultDisplay()
.getHeight() / 2, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(150);
Typeface chops = Typeface.createFromAsset(this.getAssets(),
"WC_Speed_Bold_Bta.ttf");
paint.setTypeface(chops);
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint
.ascent()) / 2));
canvas.drawText("Hello", xPos, yPos, paint);

Categories

Resources