Generate designer 2d QR code in android












2














How to generate 2-d QR Code for some text and also having an image at centre in android ? I browse a lot but i found only how to generate simple 2-d QR code using ZXing library using this link. Is it possible to generate 2-d QR Code having an image at center using ZXing library ?










share|improve this question



























    2














    How to generate 2-d QR Code for some text and also having an image at centre in android ? I browse a lot but i found only how to generate simple 2-d QR code using ZXing library using this link. Is it possible to generate 2-d QR Code having an image at center using ZXing library ?










    share|improve this question

























      2












      2








      2


      3





      How to generate 2-d QR Code for some text and also having an image at centre in android ? I browse a lot but i found only how to generate simple 2-d QR code using ZXing library using this link. Is it possible to generate 2-d QR Code having an image at center using ZXing library ?










      share|improve this question













      How to generate 2-d QR Code for some text and also having an image at centre in android ? I browse a lot but i found only how to generate simple 2-d QR code using ZXing library using this link. Is it possible to generate 2-d QR Code having an image at center using ZXing library ?







      android qr-code






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 3 '15 at 8:45









      Rahul Gupta

      555




      555
























          3 Answers
          3






          active

          oldest

          votes


















          7














          To center-align an image use code like in my activity_main.xml:



          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >

          <ImageView
          android:id="@+id/myImage"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true" />

          </RelativeLayout>


          app screenshot



          To generate and display a QR-encoded image use code like in my MainActivity.java:



          public class MainActivity extends AppCompatActivity {

          public final static int WHITE = 0xFFFFFFFF;
          public final static int BLACK = 0xFF000000;
          public final static int WIDTH = 400;
          public final static int HEIGHT = 400;
          public final static String STR = "A string to be encoded as QR code";

          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ImageView imageView = (ImageView) findViewById(R.id.myImage);
          try {
          Bitmap bitmap = encodeAsBitmap(STR);
          imageView.setImageBitmap(bitmap);
          } catch (WriterException e) {
          e.printStackTrace();
          }
          }

          Bitmap encodeAsBitmap(String str) throws WriterException {
          BitMatrix result;
          try {
          result = new MultiFormatWriter().encode(str,
          BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
          } catch (IllegalArgumentException iae) {
          // Unsupported format
          return null;
          }

          int w = result.getWidth();
          int h = result.getHeight();
          int pixels = new int[w * h];
          for (int y = 0; y < h; y++) {
          int offset = y * w;
          for (int x = 0; x < w; x++) {
          pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
          }
          }

          Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
          bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
          return bitmap;
          }
          }


          In Android Studio add the following line to build.gradle file:



          dependencies {
          ....
          compile 'com.google.zxing:core:3.2.1'
          }


          Or - if still using Eclipse with ADT plugin add core.jar by ZXing to the libs subdir (here fullscreen):



          Eclipse screenshot






          share|improve this answer























          • Alex, TS asked about in-QR image, not how to center an image
            – djdance
            Mar 9 '17 at 18:00






          • 1




            not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
            – djdance
            Mar 10 '17 at 7:15



















          0














          Personally I use this library



          Thats how you can generate QR code with that




          Java




          Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
          ImageView myImage = (ImageView) findViewById(R.id.imageView);
          myImage.setImageBitmap(myBitmap);



          Kotlin




          val qrImage = findViewById<ImageView>(R.id.img_qr_code)
          val myBitmap = QRCode.from("www.example.org").bitmap()
          qrImage.setImageBitmap(myBitmap)





          share|improve this answer





























            -1














            For whose ineteresting in the same, explore this project



            This is not a code of mine, but I've checked it and it works fine.



            The main idea is in QRCodeUtil. It's just simple overlay. Unfortunately, no theory limitation provided.



                private static Bitmap addLogo(Bitmap src, Bitmap logo) {
            if (src == null) {
            return null;
            }

            if (logo == null) {
            return src;
            }

            //获取图片的宽高
            int srcWidth = src.getWidth();
            int srcHeight = src.getHeight();
            int logoWidth = logo.getWidth();
            int logoHeight = logo.getHeight();

            if (srcWidth == 0 || srcHeight == 0) {
            return null;
            }

            if (logoWidth == 0 || logoHeight == 0) {
            return src;
            }

            //logo大小为二维码整体大小的1/5
            float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
            Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
            try {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(src, 0, 0, null);
            canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
            canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

            canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.restore();
            } catch (Exception e) {
            bitmap = null;
            e.getStackTrace();
            }

            return bitmap;
            }





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28827407%2fgenerate-designer-2d-qr-code-in-android%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              To center-align an image use code like in my activity_main.xml:



              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >

              <ImageView
              android:id="@+id/myImage"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true" />

              </RelativeLayout>


              app screenshot



              To generate and display a QR-encoded image use code like in my MainActivity.java:



              public class MainActivity extends AppCompatActivity {

              public final static int WHITE = 0xFFFFFFFF;
              public final static int BLACK = 0xFF000000;
              public final static int WIDTH = 400;
              public final static int HEIGHT = 400;
              public final static String STR = "A string to be encoded as QR code";

              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ImageView imageView = (ImageView) findViewById(R.id.myImage);
              try {
              Bitmap bitmap = encodeAsBitmap(STR);
              imageView.setImageBitmap(bitmap);
              } catch (WriterException e) {
              e.printStackTrace();
              }
              }

              Bitmap encodeAsBitmap(String str) throws WriterException {
              BitMatrix result;
              try {
              result = new MultiFormatWriter().encode(str,
              BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
              } catch (IllegalArgumentException iae) {
              // Unsupported format
              return null;
              }

              int w = result.getWidth();
              int h = result.getHeight();
              int pixels = new int[w * h];
              for (int y = 0; y < h; y++) {
              int offset = y * w;
              for (int x = 0; x < w; x++) {
              pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
              }
              }

              Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
              return bitmap;
              }
              }


              In Android Studio add the following line to build.gradle file:



              dependencies {
              ....
              compile 'com.google.zxing:core:3.2.1'
              }


              Or - if still using Eclipse with ADT plugin add core.jar by ZXing to the libs subdir (here fullscreen):



              Eclipse screenshot






              share|improve this answer























              • Alex, TS asked about in-QR image, not how to center an image
                – djdance
                Mar 9 '17 at 18:00






              • 1




                not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
                – djdance
                Mar 10 '17 at 7:15
















              7














              To center-align an image use code like in my activity_main.xml:



              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >

              <ImageView
              android:id="@+id/myImage"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true" />

              </RelativeLayout>


              app screenshot



              To generate and display a QR-encoded image use code like in my MainActivity.java:



              public class MainActivity extends AppCompatActivity {

              public final static int WHITE = 0xFFFFFFFF;
              public final static int BLACK = 0xFF000000;
              public final static int WIDTH = 400;
              public final static int HEIGHT = 400;
              public final static String STR = "A string to be encoded as QR code";

              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ImageView imageView = (ImageView) findViewById(R.id.myImage);
              try {
              Bitmap bitmap = encodeAsBitmap(STR);
              imageView.setImageBitmap(bitmap);
              } catch (WriterException e) {
              e.printStackTrace();
              }
              }

              Bitmap encodeAsBitmap(String str) throws WriterException {
              BitMatrix result;
              try {
              result = new MultiFormatWriter().encode(str,
              BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
              } catch (IllegalArgumentException iae) {
              // Unsupported format
              return null;
              }

              int w = result.getWidth();
              int h = result.getHeight();
              int pixels = new int[w * h];
              for (int y = 0; y < h; y++) {
              int offset = y * w;
              for (int x = 0; x < w; x++) {
              pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
              }
              }

              Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
              return bitmap;
              }
              }


              In Android Studio add the following line to build.gradle file:



              dependencies {
              ....
              compile 'com.google.zxing:core:3.2.1'
              }


              Or - if still using Eclipse with ADT plugin add core.jar by ZXing to the libs subdir (here fullscreen):



              Eclipse screenshot






              share|improve this answer























              • Alex, TS asked about in-QR image, not how to center an image
                – djdance
                Mar 9 '17 at 18:00






              • 1




                not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
                – djdance
                Mar 10 '17 at 7:15














              7












              7








              7






              To center-align an image use code like in my activity_main.xml:



              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >

              <ImageView
              android:id="@+id/myImage"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true" />

              </RelativeLayout>


              app screenshot



              To generate and display a QR-encoded image use code like in my MainActivity.java:



              public class MainActivity extends AppCompatActivity {

              public final static int WHITE = 0xFFFFFFFF;
              public final static int BLACK = 0xFF000000;
              public final static int WIDTH = 400;
              public final static int HEIGHT = 400;
              public final static String STR = "A string to be encoded as QR code";

              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ImageView imageView = (ImageView) findViewById(R.id.myImage);
              try {
              Bitmap bitmap = encodeAsBitmap(STR);
              imageView.setImageBitmap(bitmap);
              } catch (WriterException e) {
              e.printStackTrace();
              }
              }

              Bitmap encodeAsBitmap(String str) throws WriterException {
              BitMatrix result;
              try {
              result = new MultiFormatWriter().encode(str,
              BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
              } catch (IllegalArgumentException iae) {
              // Unsupported format
              return null;
              }

              int w = result.getWidth();
              int h = result.getHeight();
              int pixels = new int[w * h];
              for (int y = 0; y < h; y++) {
              int offset = y * w;
              for (int x = 0; x < w; x++) {
              pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
              }
              }

              Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
              return bitmap;
              }
              }


              In Android Studio add the following line to build.gradle file:



              dependencies {
              ....
              compile 'com.google.zxing:core:3.2.1'
              }


              Or - if still using Eclipse with ADT plugin add core.jar by ZXing to the libs subdir (here fullscreen):



              Eclipse screenshot






              share|improve this answer














              To center-align an image use code like in my activity_main.xml:



              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >

              <ImageView
              android:id="@+id/myImage"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true" />

              </RelativeLayout>


              app screenshot



              To generate and display a QR-encoded image use code like in my MainActivity.java:



              public class MainActivity extends AppCompatActivity {

              public final static int WHITE = 0xFFFFFFFF;
              public final static int BLACK = 0xFF000000;
              public final static int WIDTH = 400;
              public final static int HEIGHT = 400;
              public final static String STR = "A string to be encoded as QR code";

              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ImageView imageView = (ImageView) findViewById(R.id.myImage);
              try {
              Bitmap bitmap = encodeAsBitmap(STR);
              imageView.setImageBitmap(bitmap);
              } catch (WriterException e) {
              e.printStackTrace();
              }
              }

              Bitmap encodeAsBitmap(String str) throws WriterException {
              BitMatrix result;
              try {
              result = new MultiFormatWriter().encode(str,
              BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
              } catch (IllegalArgumentException iae) {
              // Unsupported format
              return null;
              }

              int w = result.getWidth();
              int h = result.getHeight();
              int pixels = new int[w * h];
              for (int y = 0; y < h; y++) {
              int offset = y * w;
              for (int x = 0; x < w; x++) {
              pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
              }
              }

              Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
              return bitmap;
              }
              }


              In Android Studio add the following line to build.gradle file:



              dependencies {
              ....
              compile 'com.google.zxing:core:3.2.1'
              }


              Or - if still using Eclipse with ADT plugin add core.jar by ZXing to the libs subdir (here fullscreen):



              Eclipse screenshot







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 7 '15 at 9:42

























              answered May 29 '15 at 12:22









              Alexander Farber

              8,19055187328




              8,19055187328












              • Alex, TS asked about in-QR image, not how to center an image
                – djdance
                Mar 9 '17 at 18:00






              • 1




                not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
                – djdance
                Mar 10 '17 at 7:15


















              • Alex, TS asked about in-QR image, not how to center an image
                – djdance
                Mar 9 '17 at 18:00






              • 1




                not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
                – djdance
                Mar 10 '17 at 7:15
















              Alex, TS asked about in-QR image, not how to center an image
              – djdance
              Mar 9 '17 at 18:00




              Alex, TS asked about in-QR image, not how to center an image
              – djdance
              Mar 9 '17 at 18:00




              1




              1




              not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
              – djdance
              Mar 10 '17 at 7:15




              not me, but TS - talked about overlay image, here is a right idea github.com/wshychbydh/Qrcode
              – djdance
              Mar 10 '17 at 7:15













              0














              Personally I use this library



              Thats how you can generate QR code with that




              Java




              Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
              ImageView myImage = (ImageView) findViewById(R.id.imageView);
              myImage.setImageBitmap(myBitmap);



              Kotlin




              val qrImage = findViewById<ImageView>(R.id.img_qr_code)
              val myBitmap = QRCode.from("www.example.org").bitmap()
              qrImage.setImageBitmap(myBitmap)





              share|improve this answer


























                0














                Personally I use this library



                Thats how you can generate QR code with that




                Java




                Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
                ImageView myImage = (ImageView) findViewById(R.id.imageView);
                myImage.setImageBitmap(myBitmap);



                Kotlin




                val qrImage = findViewById<ImageView>(R.id.img_qr_code)
                val myBitmap = QRCode.from("www.example.org").bitmap()
                qrImage.setImageBitmap(myBitmap)





                share|improve this answer
























                  0












                  0








                  0






                  Personally I use this library



                  Thats how you can generate QR code with that




                  Java




                  Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
                  ImageView myImage = (ImageView) findViewById(R.id.imageView);
                  myImage.setImageBitmap(myBitmap);



                  Kotlin




                  val qrImage = findViewById<ImageView>(R.id.img_qr_code)
                  val myBitmap = QRCode.from("www.example.org").bitmap()
                  qrImage.setImageBitmap(myBitmap)





                  share|improve this answer












                  Personally I use this library



                  Thats how you can generate QR code with that




                  Java




                  Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
                  ImageView myImage = (ImageView) findViewById(R.id.imageView);
                  myImage.setImageBitmap(myBitmap);



                  Kotlin




                  val qrImage = findViewById<ImageView>(R.id.img_qr_code)
                  val myBitmap = QRCode.from("www.example.org").bitmap()
                  qrImage.setImageBitmap(myBitmap)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 10:03









                  Zohab Ali

                  1,3111122




                  1,3111122























                      -1














                      For whose ineteresting in the same, explore this project



                      This is not a code of mine, but I've checked it and it works fine.



                      The main idea is in QRCodeUtil. It's just simple overlay. Unfortunately, no theory limitation provided.



                          private static Bitmap addLogo(Bitmap src, Bitmap logo) {
                      if (src == null) {
                      return null;
                      }

                      if (logo == null) {
                      return src;
                      }

                      //获取图片的宽高
                      int srcWidth = src.getWidth();
                      int srcHeight = src.getHeight();
                      int logoWidth = logo.getWidth();
                      int logoHeight = logo.getHeight();

                      if (srcWidth == 0 || srcHeight == 0) {
                      return null;
                      }

                      if (logoWidth == 0 || logoHeight == 0) {
                      return src;
                      }

                      //logo大小为二维码整体大小的1/5
                      float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
                      Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
                      try {
                      Canvas canvas = new Canvas(bitmap);
                      canvas.drawBitmap(src, 0, 0, null);
                      canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
                      canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

                      canvas.save(Canvas.ALL_SAVE_FLAG);
                      canvas.restore();
                      } catch (Exception e) {
                      bitmap = null;
                      e.getStackTrace();
                      }

                      return bitmap;
                      }





                      share|improve this answer


























                        -1














                        For whose ineteresting in the same, explore this project



                        This is not a code of mine, but I've checked it and it works fine.



                        The main idea is in QRCodeUtil. It's just simple overlay. Unfortunately, no theory limitation provided.



                            private static Bitmap addLogo(Bitmap src, Bitmap logo) {
                        if (src == null) {
                        return null;
                        }

                        if (logo == null) {
                        return src;
                        }

                        //获取图片的宽高
                        int srcWidth = src.getWidth();
                        int srcHeight = src.getHeight();
                        int logoWidth = logo.getWidth();
                        int logoHeight = logo.getHeight();

                        if (srcWidth == 0 || srcHeight == 0) {
                        return null;
                        }

                        if (logoWidth == 0 || logoHeight == 0) {
                        return src;
                        }

                        //logo大小为二维码整体大小的1/5
                        float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
                        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
                        try {
                        Canvas canvas = new Canvas(bitmap);
                        canvas.drawBitmap(src, 0, 0, null);
                        canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
                        canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

                        canvas.save(Canvas.ALL_SAVE_FLAG);
                        canvas.restore();
                        } catch (Exception e) {
                        bitmap = null;
                        e.getStackTrace();
                        }

                        return bitmap;
                        }





                        share|improve this answer
























                          -1












                          -1








                          -1






                          For whose ineteresting in the same, explore this project



                          This is not a code of mine, but I've checked it and it works fine.



                          The main idea is in QRCodeUtil. It's just simple overlay. Unfortunately, no theory limitation provided.



                              private static Bitmap addLogo(Bitmap src, Bitmap logo) {
                          if (src == null) {
                          return null;
                          }

                          if (logo == null) {
                          return src;
                          }

                          //获取图片的宽高
                          int srcWidth = src.getWidth();
                          int srcHeight = src.getHeight();
                          int logoWidth = logo.getWidth();
                          int logoHeight = logo.getHeight();

                          if (srcWidth == 0 || srcHeight == 0) {
                          return null;
                          }

                          if (logoWidth == 0 || logoHeight == 0) {
                          return src;
                          }

                          //logo大小为二维码整体大小的1/5
                          float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
                          Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
                          try {
                          Canvas canvas = new Canvas(bitmap);
                          canvas.drawBitmap(src, 0, 0, null);
                          canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
                          canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

                          canvas.save(Canvas.ALL_SAVE_FLAG);
                          canvas.restore();
                          } catch (Exception e) {
                          bitmap = null;
                          e.getStackTrace();
                          }

                          return bitmap;
                          }





                          share|improve this answer












                          For whose ineteresting in the same, explore this project



                          This is not a code of mine, but I've checked it and it works fine.



                          The main idea is in QRCodeUtil. It's just simple overlay. Unfortunately, no theory limitation provided.



                              private static Bitmap addLogo(Bitmap src, Bitmap logo) {
                          if (src == null) {
                          return null;
                          }

                          if (logo == null) {
                          return src;
                          }

                          //获取图片的宽高
                          int srcWidth = src.getWidth();
                          int srcHeight = src.getHeight();
                          int logoWidth = logo.getWidth();
                          int logoHeight = logo.getHeight();

                          if (srcWidth == 0 || srcHeight == 0) {
                          return null;
                          }

                          if (logoWidth == 0 || logoHeight == 0) {
                          return src;
                          }

                          //logo大小为二维码整体大小的1/5
                          float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
                          Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
                          try {
                          Canvas canvas = new Canvas(bitmap);
                          canvas.drawBitmap(src, 0, 0, null);
                          canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
                          canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

                          canvas.save(Canvas.ALL_SAVE_FLAG);
                          canvas.restore();
                          } catch (Exception e) {
                          bitmap = null;
                          e.getStackTrace();
                          }

                          return bitmap;
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 10 '17 at 7:18









                          djdance

                          2,2281926




                          2,2281926






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28827407%2fgenerate-designer-2d-qr-code-in-android%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              TypeError: fit_transform() missing 1 required positional argument: 'X'