How to generate a 24bit Bitmap in android - java

I am working on android app that needs to generate a QRCode, and I successfully done with this.
My problem is on the printing side. I used a Mobiprint 3 device which has a built-in Thermal Printer. But my problem is the device printer only support a 24bit Bitmap.
My question is, is there a way to create a 24bit Bitmap in android? since it only support a 32bit. I googled it in a week but no one solved my problem.
Thank you in advance.

BTW.
This is my code
//method for generating a QRCode Bitmap
try {
bitmap = qrGenerator.generateQRCode(duCode);
int width, height;
height = bitmap.getHeight();
width = bitmap.getWidth();
bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
} catch (WriterException e) {
e.printStackTrace();
}
}
and this is when calling the Bitmap for printing.
print.printBitmap(getBitmap());
this codes only prints a pure square black.
PS: print.printBitmap() is from the Mobiprint API.

Related

Porterduff Modes(Multiply)

I wanted to use porterduff multiplication mode to the PNG image, but the background became Black, only the background in the PNG extension picture becomes Black, how can I fix this?
The background should be transparent when I use the multiplication mode I want.
This line of code did not work:
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I myself wrote code as below but it didn't work and didn't show any pictures.
private Bitmap MultiplyBitmap(Bitmap bitmapmultiply){
Bitmap bitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas cnvs = new Canvas(bitmap);
Paint pnt = new Paint();
pnt.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
cnvs.drawBitmap(bitmapmultiply, 0, 0, pnt);
Bitmap multiplybitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(multiplybitmap);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapmultiply,0,0, null);
canvas.drawBitmap(bitmap, 0, 0, paint);
return multiplybitmap;
}
How can I remove the black background using Java? Thanks
Please, refer to the that PoterDuff.Mode link from Android webstie where you can find a lot of examples.
You could try something like this (just adapt the code for your needs):
Kotlin
val bmpDrawable = bmp.toDrawable(res)
bmpDrawable = DrawableCompat.setTint(bmpDrawable, Color.BLACK)
val tintedBmp = bmpDrawable.bitmap
return jokeDao.insert(entity.toDbJoke())
Java
Drawable bmpDrawable = bmp.toDrawable(res)
DrawableCompat.setTint(bmpDrawable, Color.BLACK)
Bitmap tintedBmp = bmpDrawable.bitmap

write a custom name on the top of the picture taken by android cam

If this question is repeated then let me know the link of original question because i enable to findout the good link for resolve my current problem.
I am working on android camera I able to take Picture's from my app. But I want to write name on the top of taken picture. i enable to find out how can i resolve this issue.
Sorry for i don't have any code for take reference.....
any help will be appreciated and i want to pay my thank in advance to all of you.
Try following code.
public Bitmap drawTextToBitmap(Bitmap bitmap, String mText) {
try {
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(110,110, 110));
// text size in pixels
paint.setTextSize((int) (12 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/6;
int y = (bitmap.getHeight() + bounds.height())/5;
canvas.drawText(mText, x * scale, y * scale, paint);
return bitmap;
} catch (Exception e) {
return null;
}
}

Decode specific areas of image in Bitmapfactory?

I'm working with GeoTiff/PNG files too large for handling as a whole in my code.
Is there any possibility to decode specific areas (e.g. given by two x,y coordinates) of a file in bitmapfactory? Haven't found anything looking similar at http://developer.android.com/reference/android/graphics/BitmapFactory.html(Android's developer reference).
Thanks!
With kcoppock's hint I've set up the following solution.
Though I'm wondering why rect needs to be initialized by Rect(left, bottom, right, top) instead of Rect(left, top, right, bottom)...
Example call:
Bitmap myBitmap = loadBitmapRegion(context, R.drawable.heightmap,
0.08f, 0.32f, 0.13f, 0.27f);
Function:
public static Bitmap loadBitmapRegion(
Context context, int resourceID,
float regionLeft, float regionTop,
float regionRight, float regionBottom) {
// Get input stream for resource
InputStream is = context.getResources().openRawResource(resourceID);
// Set options
BitmapFactory.Options opt = new BitmapFactory.Options();
//opt.inPreferredConfig = Bitmap.Config.ARGB_8888; //standard
// Create decoder
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(is, false);
} catch (IOException e) {
e.printStackTrace();
}
// Get resource dimensions
int h = decoder.getHeight();
int w = decoder.getWidth();
// Set region to decode
Rect region = new Rect(
Math.round(regionLeft*w), Math.round(regionBottom*h),
Math.round(regionRight*w), Math.round(regionTop*h));
// Return bitmap
return decoder.decodeRegion(region, opt);
}
You should look into BitmapRegionDecoder. It seems to describe exactly the use case that you are looking for.
I don't know exactly what you mean by "Decode specific areas" but if by decoding you mean, to actually "copy" certain areas of a bitmap, what you can do is make use of canvas in order to get it as shown below:
Bitmap bmpWithArea = Bitmap.createBitmap(widthDesired, heightDesired, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpWithArea);
Rect area = new Rect(arealeft, areatop, arearight, areabottom);
Rect actualSize = new Rect(0, 0, widthDesired, heightDesired);
canvas.drawBitmap(bitmapWithAreaYouWantToGet, area, actual, paintIfAny);
//And done, starting from this line "bmpWithArea" has the bmp that you wanted, you can assign it to ImageView and use it as regular bmp...
Hope this helps...
Regards!

Create a removable canvas overlay

Im having two bitmaps as I want to blend together.
Im using a canvas to achieve this. The following code will create a resulting image where the mask is 50% blended into to background.
Bitmap output = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Config.ARGB_8888);
Paint p = new Paint();
Paint maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
maskPaint.setAlpha(127);
Canvas c = new Canvas(output);
c.drawBitmap(picture, 0, 0, p);
c.drawBitmap(mask, 0, 0, maskPaint);
return output;
I have also been expermenting if im able to remove parts of the bitmap, using Xfermode. I have done this with the following code:(This will create a hole, a square)
int height = BitmapHandler.getMainBitmap().getHeight();
int width = BitmapHandler.getMainBitmap().getWidth();
Bitmap bmOverlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(BitmapHandler.getMainBitmap(), 0, 0, null);
c.drawRect(30, 30, 100, 100, p);
return bmOverlay;
Now, Im wondering if, using a canvas, I am able to draw a background and a mask and at the same time being able to remove parts of the mask and let the background "shine" through.
Thanks!

Overlay images in Android

I have two images that I want to merge into one. (Eg "House.png" on top of "street.png")
How do i achieve this in Android? I just want to merge the images and export them to a file.
This example Sets the image to an ImageView but i wish to export it.
This other example does not work in Android since the classes are not available.
I'd try something like:
public static Bitmap mergeImages(Bitmap bottomImage, Bitmap topImage) {
final Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage
.getHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bottomImage, 0, 0, paint);
canvas.drawBitmap(topImage, 0, 0, paint);
return output;
}
(not tested, I just wrote it here, might be some simple errors in there)
Basically what you do is create a 3rd empty bitmap, draw the bottom image on it and then draw the top image over it.
As for saving to a file, here are a few examples: Save bitmap to location
You can do like this...............
public Bitmap Overlay(Bitmap Bitmap1, Resources paramResources, Bitmap Bitmap2, int alpha)
{
Bitmap bmp1 = Bitmap.createScaledBitmap(Bitmap2, Bitmap1.getWidth(), Bitmap1.getHeight(), true);
Bitmap bmp2 = Bitmap.createBitmap(Bitmap1.getWidth(), Bitmap1.getHeight(), Bitmap1.getConfig());
Paint localPaint = new Paint();
localPaint.setAlpha(alpha);
Canvas localCanvas = new Canvas(bmp2);
Matrix localMatrix = new Matrix();
localCanvas.drawBitmap(Bitmap1, localMatrix, null);
localCanvas.drawBitmap(bmp1, localMatrix, localPaint);
bmp1.recycle();
System.gc();
return bmp2;
}

Categories

Resources