我如何在手机中保存生成的QR码扫描仪图像

时间:2020-07-21 08:54:21

标签: java qr-code

我正在尝试制作一个名为QR码扫描仪的应用程序并生成。 我被困在如何保存生成的QR码图像上,我试图在youtube和google中进行搜索,但没有找到与此主题相关的任何指南。 我通过在按钮中添加代码来尝试将自己保存到设备中,从而可以将图像保存到设备中,但是当我运行应用程序并单击下载时,应用程序崩溃了,logcat并没有显示太多信息 如果有人可以帮助我或向我展示可以学到这东西的任何视频,将不胜感激

这是generate.java

    public class Generate extends AppCompatActivity {

Button button_generate_now;
EditText editText;
ImageView img_scan_result;
ImageButton download_scan;
FileOutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_generate);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


    button_generate_now = findViewById(R.id.button_generate_now);
    editText = findViewById(R.id.editText);
    img_scan_result = findViewById(R.id.img_scan_result);
    download_scan = findViewById(R.id.download_scan);



        button_generate_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRCodeWriter qrCodeWriter = new QRCodeWriter();
                try {
                    BitMatrix matrix = qrCodeWriter.encode(editText.getText().toString(), BarcodeFormat.QR_CODE, 200, 200);
                    final Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.RGB_565);
                    for (int x = 0; x < 200; x++) {
                        for (int y = 0; y < 200; y++) {
                            bitmap.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
                        }
                    }
                    img_scan_result.setImageBitmap(bitmap);

                    download_scan.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BitmapDrawable drawable = (BitmapDrawable) img_scan_result.getDrawable();
                            Bitmap bitmap1 = drawable.getBitmap();
                            File filepath = Environment.getExternalStorageDirectory();
                            File dir = new File(filepath.getAbsolutePath()+"/Demo");
                            dir.mkdir();
                            File file = new File(dir,System.currentTimeMillis()+"jpg");
                            try {
                                outputStream = new FileOutputStream(file);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            bitmap1.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                            Toast.makeText(getApplicationContext(),"Image saved to device",Toast.LENGTH_LONG).show();
                            try {
                                outputStream.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            try {
                                outputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (WriterException e) {
                    e.printStackTrace();
                }
            }
        });
}

0 个答案:

没有答案