I am generating a QR code with ZXing library in an Android app.
Instead of directly saving it into a file, I want it to go into an ImageView.
Here is my code:
String WIFIQRCODE = "";
String SSID = etSSID.getText().toString();
String PASS = etPASS.getText().toString();
String PASSTYPE = sTYPE.getSelectedItem().toString();
WIFIQRCODE = "WIFI:T:"+PASSTYPE+";S:"+SSID+";P:"+PASS;
//Inform the user the button1 has been clicked
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(WIFIQRCODE, BarcodeFormat.QR_CODE, 300, 300);
File file = new File(context.getFilesDir(), "/sdcard/Images/"+SSID+".png");
MatrixToImageWriter.writeToFile(bitMatrix, "png", file);
} catch (WriterException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
How to modify it, so that it puts it into an ImageView instead of a file?
you will need to get Bitmap from BitMatrix to set directly image in ImageView do it as:
int height = bitMatrix.getHeight();
int width = bitMatrix.getWidth();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++){
bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
}
}
ImageView qr_image = (ImageView) findViewById(R.id.qrimage);
qr_image.setImageBitmap(bmp);
for more detail you can see Generating QR Codes with ZXing for getting Bitmap from bitMatrix
simple way
public static Bitmap createQRImage(final String url, final int width, final int height) throws WriterException {
final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
return Bitmap.createBitmap(IntStream.range(0, height).flatMap(h -> IntStream.range(0, width).map(w -> bitMatrix.get(w, h) ? Color.BLACK : Color.WHITE)).toArray(),
width, height, Bitmap.Config.ARGB_8888);
}
or
public static Bitmap createQRImage2(final String url, final int width, final int height) throws WriterException {
final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
return Bitmap.createBitmap(IntStream.range(0, height * width).map(s -> bitMatrix.get(s % width, s / width) ? Color.BLACK : Color.WHITE).toArray(),
width, height, Bitmap.Config.ARGB_8888);
}
or, because Stream.toArray() allocate the output array dynamically if size unknown, which may cause memory allocation frequently if the array size too large
public static Bitmap createQRImage3(final String url, final int width, final int height) throws WriterException {
final BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, Collections.singletonMap(EncodeHintType.CHARACTER_SET, "utf-8"));
return Bitmap.createBitmap(IntStream.range(0, height)
.flatMap(h -> IntStream.range(0, width).map(w -> bitMatrix.get(w, h) ? Color.BLACK : Color.WHITE))
.collect(() -> IntBuffer.allocate(width * height), IntBuffer::put, IntBuffer::put)
.array(),
width, height, Bitmap.Config.ARGB_8888);
}
As appose to that,
get the directory to the image file then feed it in to:
Bitmap imageBitmap = BitmapFactory.decodeFile(path);
then its simple as
myImageView.setImageBitmap(imageBitmap);
Related
I'm trying to draw a bitmap on the top of another bitmap like this :
I'm using the following code to create an empty background with 420x420 as size, and draw the star on it :
for (int i = 0; i < 10; i++) {
Bitmap resized;
if (stretchedPosition.contains(i)) {
resizedStar = Bitmap.createScaledBitmap(star, star.getWidth() - 80, star.getHeight() + 80, true);
} else
resizedStar = Bitmap.createScaledBitmap(star, star.getWidth() + 80, star.getHeight() - 80, true);
resized = makeBackground(resized);
//code for generating a GIF from bitmaps
}
public Bitmap makeBackground(Bitmap resized) {
Bitmap emptyBitmap = Bitmap.createBitmap(420, 420, Bitmap.Config.ARGB_8888);
int positionLeft = 0;
int positionTop = 0;
Bitmap newBitmap = Bitmap.createBitmap(emptyBitmap.getWidth(), emptyBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(emptyBitmap, positionLeft, positionTop, null);
canvas.drawColor(Color.GREEN);
int bitmap1Width = resized.getWidth();
int bitmap1Height = resized.getHeight();
int bitmap2Width = emptyBitmap.getWidth();
int bitmap2Height = emptyBitmap.getHeight();
float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);
canvas.drawBitmap(resized, new Matrix(), null);
canvas.drawBitmap(emptyBitmap, marginLeft, marginTop, null);
return newBitmap;
}
THE ISSUE:
As you can see here, the girl image is not centered and the image gets cut as well.
This may helps you;
Bitmap fileBitmap = BitmapFactory.decodeFile(filePath);
Bitmap blankBitmap = getBlankBitmap(YOUR_WIDTH, YOUR_HEIGHT);
Bitmap mergeBitmap = overlay(blankBitmap, fileBitmap);
you can generate blank bitmap using following method:
public static Bitmap getBlankBitmap(int w, int h) {
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);
return bmp;
}
and Merge two bitmap like this;
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
int padding = (bmp1.getWidth() / 2) - (bmp2.getWidth() / 2);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, padding, 0, null);
bmp1.recycle();
bmp2.recycle();
return bmOverlay;
}
Im trying to save my bitmap file as to png to my Internal Storage
How can i do it?
My code to generate QR Code :-
public void createQRCode(String qrCodeData, String charset, Map hintMap, int qrCodeheight, int qrCodewidth) {
Bitmap bitmap = null;
try {
//generating qr code in bitmatrix type
BitMatrix matrix = new MultiFormatWriter().encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
//converting bitmatrix to bitmap
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
qrImageView.setImageBitmap(bitmap);
} catch (Exception er) {
Log.e("QrGenerate", er.getMessage());
}
}
I want to save the generated QR code bitmap to my Internal Storage for example say in DCIM folder i want make a new folder "QR CODES" then i want to save my bitmap as "QRCode1.png"
How can i do this?
i have tried many tutorials and none of them seem to work for me or i am doing it wrong maybe
PS
i have already added the write permission to my AndroidManifest file
Try doing this and then use the data to save it in your local device.
saveImageBitmap(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte[] data = baos.toByteArray();
//do whatever you want to save this bitmap file
}
Hope that'll help you out! This will transform the bitmap into .jpg format and then save it in your file.
I have this method to generate a Barcode bitmap:
public static Bitmap encodeToQrCode(String text, int width, int height) {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = null;
try {
matrix = writer.encode(text, BarcodeFormat.QR_CODE, 400, 400);
} catch (WriterException ex) {
ex.printStackTrace();
}
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height - 1; y++) {
bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
}
Is there any way to find out that the Barcode has been scanned? I'm using zxing.
If I understand you correctly you're looking for a way to
Create a bitmap that contains a QR code
Display that bitmap on your device
Detect when a different device that doesn't use your software scans that code
This is not possible. The QR code bitmap is just that: A bitmap your app displays. And a barcode scanner app simply takes a photo of your display and tries to find patterns in that bitmap.
i wanna make a thunbnail image(resized image)
this is my code
public static void createImage(String loadFile, String saveFile)throws IOException{
File load_image = new File(loadFile); //가져오는거
FileInputStream fis = new FileInputStream(load_image);
File save = new File(saveFile); // 썸네일
BufferedImage bi = ImageIO.read(fis);
int width = bi.getWidth();
int height = bi.getHeight();
int maxWidth=0;
int maxHeight=0;
if(width>height){
maxWidth = 1280;
maxHeight = 720;
}else{
maxWidth = 720;
maxHeight = 1280;
}
if(width > maxWidth){
float widthRatio = maxWidth/(float)width;
width = (int)(width*widthRatio);
height = (int)(height*widthRatio);
}
if(height > maxHeight){
float heightRatio = maxHeight/(float)height;
width = (int)(width*heightRatio);
height = (int)(height*heightRatio);
}
BufferedImage thu = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = thu.createGraphics();
g2.drawImage(bi, 0, 0, width, height, null);
ImageIO.write(thu, "jpg", save);
}
sometimes my image colors are changed with unexpected colors
this is image example
first is origin
second is thumbnails
i don't know why...
where i mistake??
help me please...
You write your image using BufferedImage.TYPE_INT_RGB. But if this is not the type of the source image, the colors get wrong.
Try replacing this line
BufferedImage thu = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
by this:
BufferedImage thu = new BufferedImage(width, height, bi.getType());
I want to merge two bitmaps side-by-side into one bitmap. The following code is merge sub-bottom. How do I merge side-by-side into one bitmap ?
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc)
{
Bitmap comboBitmap;
int width, height;
width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();
comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(comboBitmap);
comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, 0f , fr.getHeight(), null);
return comboBitmap;
}
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc)
{
Bitmap comboBitmap;
int width, height;
width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();
comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(comboBitmap);
comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, fr.getWidth(), 0f , null);
return comboBitmap;
}
This article goes through the process of combining 2 images one below the other(only works with PNG or JPG ). It will involve passing 2 Bitmaps, which will then get combined using the Canvas class. You can do somme minor changes to get your two images side by side:
public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom
Bitmap cs = null;
int width, height = 0;
if(c.getHeight() > s.getHeight()) {
width = c.getWidth() + s.getWidth(;
height = c.getHeight());
} else {
width = c.getWidth() + s.getWidth();
height = s.getHeight();
}
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, c.getWidth(), 0f, null);
// this is an extra bit I added, just incase you want to save the new image somewhere and then return the location
/*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(loc + tmpImg);
cs.compress(CompressFormat.PNG, 100, os);
} catch(IOException e) {
Log.e("combineImages", "problem combining images", e);
}*/
return cs;
}