生成并下载二维码

package com.okni.okpool.okfinance.util;

import com.aliyun.oss.OSSClient;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.okni.okpool.okfinance.config.BootdoConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Component
public class DownloadUtil {
    @Autowired
    private BootdoConfig bootdoConfig;
    /**
     * 生成并下载二维码
     * @param url 二维码对于URL
     * @param width 二维码宽
     * @param height 二维码高
     * @param format  二维码格式
     * @return
     * @throws WriterException
     * @throws IOException
     */

    public ResponseEntity<byte[]> getResponseEntity(String url, int width, int height, String format) throws WriterException, IOException {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        //将矩阵转为Image
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, format, out);//将BufferedImage转成out输出流
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(out.toByteArray(),
                headers, HttpStatus.CREATED);
    }
    public void upload(String fileName, InputStream file){
        // Endpoint以杭州为例,其它Region请按实际情况填写。
        String endpoint = "https://"+bootdoConfig.getEndpoint();
        // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = bootdoConfig.getAccessKeyId();
        String accessKeySecret = bootdoConfig.getAccessKeySecret();
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
        ossClient.putObject(bootdoConfig.getBucketName(), fileName, file);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
    public  void createInternetShortcut(String name, String where, String target, String icon)throws IOException{
//        FileWriter fw = new FileWriter(where);
//        fw.write("[InternetShortcut]\n");
//        fw.write("URL=" + target + "\n");
//        if (!icon.equals(""))  {
//            fw.write("IconFile=" + icon + "\n");
//        }
//        fw.flush();
//        fw.close();


    }
    public InputStream parse(ByteArrayOutputStream out) throws Exception
    {
        ByteArrayOutputStream   baos=new   ByteArrayOutputStream();
        baos=(ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }
    public static void main(String[] args) throws Exception {
        DownloadUtil downloadUtil=new DownloadUtil();
        String cc="7b227478223a22415141414141497a4c49797556426a31696c7977793744345473363065586d477a59715452715a4e416a6e693674616764774941414141412f2f2f2f2f2b363833765459676a4d642b48636e3064356a586b626966664f4d45674b433477305873793961694a595a616741414141442f2f2f2f2f61656a73316741414141414146366b5544316c396d75766b4c69706f4c725351494341376e464e6d38525748734f76684141414141414158715251536770314b613153423773764d7143537062736d716f417a3554496531697277684141414141426570464257533467324838665266794b4a305244786f4d694164703635586879372f78674d414141414146366b55484573475a32744d3339456b673345637879677743464b2f6b66714858683476417741414141415871525165696e4a6b765141557a494874626233693333334c534462596a496472306849424141414141426570464236696e4f577330654e6771333873485a56554452494763454357682f343947774d414141414146366b554a534966796b416c6b64465a4974336136586743384d34344b525348384a476542674141414141587152517555366d534a67524d66367339344b503857733045334e62414d346636707230414141414141426570464337714c6456742b6e554d6c6b3774384e4b7367776e366d547666682b3832686751414141414146366b55537354757a4172796c364e33536931524c3631315173755a71586948734a524f41774141414141587152524c6d2b497665573455547942614777695a68337a744171324e575964492b416b4f414141414142657046464d44525a7445384130504c4e767a2b67736676535a57683677426838766c58464d414141414146366b55577138445534325266744b484331654566715967364e393830577548645a6b5242414141414141587152527143797979557568615943667157394f4442645954486c6c4c3859644831466b4f41414141414265704647316e49424f636d463143494664683163374571694f2f657542796832516a5a6751414141414146366b55673448666445597a7453755169333946776d522b726e626237634348696d41444151414141414158715253514d4156384c797679577a6f30645a4b4c6666397169775270523465775958384b4141414141426570464b574d6e426452746a6c33677a496c384d676d616246596c455a4e683037725a6749414141414146366b55745032436f58375a696a576b58786b736732417475356553696e57485a624a7043774141414141587152533668456434766a7a453345396b7a79636464575036414779575159662f536159414141414141426570464d6231465a336d736275754c6d41676e71774f3038797a3064456b683035462f6741414141414146366b557a77465535537a49346e7968474531424c7178734265355379335748315138644151414141414158715254354e6f356e317557775656776f71532b4e70665959476a4b59396f634f4f6a774141414141414265704650385134716c754e46794949784e447831716b5672436c35335772682f39424f7745414141414147586170464149615239666d41514145736c447464555437472f546362784f34694b7934414e55524141414141426c3271525145613854732f72506d38514465586732474a644d344431364c6b49697369707978424141414141415a64716b5543726f50536e4f412b69786f74445049745234374378317153524349724b414359675141414141414758617046413264694767655642486c7a556237365978544f35733370474f79694b776d366649414141414141426c327152514f446876794b7859787949585762456d6d564e6a3054645a6c6e596973505a486f414141414141415a64716b55446e4741724357766b73676f742b43627837625248654c6846477949724445384f41454141414141475861704641366c36324b61516d3647466b3044516e43553651644a6a616b38694b7978366173444141414141426c327152515063664f524a5078674e77302f5a67774559472b6c666674676c496973416f4670417741414141415a64716b55485076784638592b716a7348434f3151585258526f41743079784b49724c3965366741414141414147586170464234457566334f356b36514b77786933524343745165712f6f4b76694b7a4a735338424141414141426c327152516730494273546663356f74714a774439305a4666686663646854496973704b6b2f485141414141415a64716b55496e6c637a7279652f2f30737455514f6c4b324e5832722b69552b49724638747751454141414141475861704643583644532f2b717737536e4a4a4d356a56777571537774783669694b782b313251444141414141426c327152516d39422b576d4a6774682b525158355332634643394466774f796f697343645855416741414141415a64716b554a767241615439416a366b536b386b72616c376b554d726571695349724b41306a77414141414141475861704643656a6b696c634c72492b4f4f684b695a5733736d5a4d62326f56694b7a6b655734414141414141426c32715251734531704f485435584870456e346e644663796f6e394b4c4b306f697339767a34414141414141415a64716b554c455339724a546f476e63575267654c5a744273382b32734a536149724f626e4e674d414141414147586170464333794273566d412b6330527a3249504f52536b4a464432375357694b7844396c63414141414141426c32715251755554566e34467978734e6a717234526d484f614d3553436b3034697369313149417741414141415a64716b554c747a75537869383256524441485073426d4b62687538594c77754972414a6e636749414141414147586170464337302f58694932746b613839506b49743056397047505054704b694b7a38703273454141414141426c3271525179395a3136593754595a4f756d554e65486a7368444e2b426a3459697365584846425141414141415a64716b554e65756f33797357675a7332453658707266434176336c35304b7149724743494b4145414141414147586170464464472f503164374159516d6c494c5a692b4d6371676d6d614451694b7a34704d6e4a4141414141426c3271525136374a306e39594465475258396235717578307338734b6637743469736e7a714d435141414141415a64716b555052574c536435774732383541507a4b63787a7931574e6b52392b49724947366b67414141414141475861704644324557346a6d597a54724865574a394c4e6a504e6b795a694f76694b7858567149414141414141426c327152512b336f5a512f646c336f585865326e52483647766b6d4f56737259697358576d73415141414141415a64716b555076345544704f4470535743745432514263397951366c6c2b506549724a586156415541414141414758617046442b324e794776543474544633366165424647676d414c576d6464694b7731704834464141414141426c3271525241742b6f58644f423274754935676b3338304a362f5a7048757149697348695338415141414141415a64716b5552622f39414966656a5771674244362b6775396b424e477633466549724f452f3267454141414141475861704645665754357a5551393431643433692b67456e68566f5171685468694b774a564638414141414141426c327152524a435068392f45316e4f794267553842424f704743464a5941755969736b433358437741414141415a64716b5553646c3841432b6e3865666a52484e4e5477435a596e516330386949724445753977414141414141475861704645725551326e5673354139656c664544734253574c637848596271694b794c685841414141414141426c327152524d4c555a676e46722f3139437a4146335a547030375370724a586f697346783643415141414141415a64716b5555364b505251715849323472632f7375665630636830304b3534474972474b4a675130414141414147586170464653742f42453877543267476e593457706a6f6d5030562f797849694b7831634151424141414141426c32715252564e6a736371572f4943574d5a4635435a78676f7854386b6b5a6f69736b6d6c6d414141414141415a64716b5557323361706c6c79314651675a763975512f71636e4a5166673447497244484769776f4141414141475861704647425345756e79424967777768623068776947674c5a4d714b6731694b777a364130544141414141426c3271525267664e4c30346650597038794733595648725462774f5355744d6f697351545648464141414141415a64716b55595134387a6c68443937644f356956754234692f5649306958456149724b2b4d486773414141414147586170464768754342736b304c4d564579597647475574376949514e46526b694b7938744451424141414141426c327152527131475952746e524430734f376c4c6e563965646f64715844346f6973654b644e417741414141415a64716b556176334677397538686c7554684e30303139626f48466b4278772b497242514270785141414141414758617046477658674b4741685046756a575677687739394d7a4e337153704e694b786b694777444141414141426c32715252777372746a614b38346d6744655643375356384e734677646a4c6f697359695369415141414141415a64716b55633945493847634f42745341574766683452735731686b58325269497247515766774541414141414758617046485073753475514866644876506e6f7963304b5835495a774d5430694b773471506b444141414141426c32715252377142726a684a79696442633664414536516154424d426b69726f69737470726b417741414141415a64716b55652b474550447047686c796b594b31714e2f595775542f4e4d3253497248464f5677414141414141475861704649415552524462703452574269304573316572386d39784e68614b694b796e6e2b674a4141414141426c3271525349362b5846614e4d2b7833467553457a43314d4150414639333849697345684272437741414141415a64716b556a535564437856536f426b45374b4372524d504d547a5049375557497243705461774541414141414758617046492f37355067524d3670316a6b6b694f523047457078754e6e6c64694b776c334741414141414141426c327152535853397945714b77717469754442757a5a6374777776656255593469735a6e4c4e446741414141415a64716b556d413452793779474869372f43526742774c7552636a5a694c61694972506b51335151414141414147586170464a6b644f6c3639475a2b6a3476714755744d42325a754632326458694b7a59614e6f504141414141426c3271525361456b36414a677071507a51676a6d744f776930475066535358346973564a496e415141414141415a64716b556e4d366374393565536b39337461516a7a46424a4d336255435a614972474437326741414141414147586170464a313837545657353166512b41487a677142327a472b6370426966694b7a503439384c4141414141426c32715253654751643769706635504737354d454e70416366594f785553566f69736f6e3733414141414141415a64716b55746a67624d732b5233524634795148374b30574b6f353472537965497245474a537767414141414147586170464c756734594b5472532b522b4c346a6d4d6d425238656173456a59694b7742674f45424141414141426c32715253384d514e6d493344344a6e544c617838366c76654c676843336549697348363476414141414141415a64716b55766d736970564f61522b6950356b366a774f48323734692b75793649724c4a49587767414141414147586170464d71374f37354b34626c513771374b386e566e734d636c2f55616a694b796a4773734d4141414141426c32715254536b6752493537394b796e31366156506158415a79704f7734666f6973397a6441414141414141415a64716b55303277697738667848446274393877564e552f4c647268692f556d4972462b2b4a7749414141414147586170464e5535306e4e4e5a4c46634e423568396842496c54733164337458694b7730416855464141414141426c32715254593063732f47766e4f304a51356f4f63565a47335772716646664969736e6a4c57437741414141415a64716b553331674669586a75395153654137336a567337414c554532786f4b49724a4434495151414141414147586170464f7a6958312b71456f544e553346383374376952556f452b676d74694b7a30396d45454141414141426c32715254776d6466694a6a754a4f456f5366734173396e515556567338643469736f66554f497741414141415a64716b5538586158312b35646d7136716b7658472f6952794d5458336f38694972424633635145414141414146366b556975744752524a4d6c48412b646c694e754d355579627247754a6d484b7169374377414141414158715254694c6c79593968707a75534d645874764f64495032397a2b793249666d512f30414141414141426570464f375a4c372f614c544d4d76344e5357516d556768303872566535687741414141413d222c227574786f73223a5b7b2274786964223a2237376130643665616532333930323464613634363933386163643836373937396234636534656638623063626230356338616635313835346165386332633333222c22766f7574223a322c22616d6f756e74223a3133362c227363726970745075624b6579223a2261393134653232653563393866363161373362393233316435656462636537343833663666373366623264383837222c2272656465656d536372697074223a22353232313033386464316534616236363933623434633064346264303739363936313434383833323732646361313730346436623037303662356265653939376432666165373231303263636666313337613233303331346334333538303431333539343235653538633930346338613932346134333132333965366363333863363766303431306639323130326563666464633665663336303737366133633230646337656430356534353866653630643837353434636534623263363831643936356532346437313930306235336165227d2c7b2274786964223a2231393936383835613266623331373064653338323032313238636633376465323436356536336465643132373737663831643333383264386634646562636565222c22766f7574223a3130362c22616d6f756e74223a352c227363726970745075624b6579223a2261393134653232653563393866363161373362393233316435656462636537343833663666373366623264383837222c2272656465656d536372697074223a22353232313033386464316534616236363933623434633064346264303739363936313434383833323732646361313730346436623037303662356265653939376432666165373231303263636666313337613233303331346334333538303431333539343235653538633930346338613932346134333132333965366363333863363766303431306639323130326563666464633665663336303737366133633230646337656430356534353866653630643837353434636534623263363831643936356532346437313930306235336165227d5d7d";
        String compress = GZIPUtils.compress(cc);
        System.out.print("##"+compress+"##"+compress.length()+"===");
        //返回二维码图片流
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(compress,
                BarcodeFormat.QR_CODE, 200, 200, hints);// 生成矩阵
        //将矩阵转为Image
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "png", out);//将BufferedImage转成out输出流
        String fileName="15738963828.png";
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAImzl4bFAt85P3";
        String accessKeySecret = "JKkCmGh8rn4tJOr1Z4ELMJpmiMLt6L";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
       ossClient.putObject("okminerimg", fileName, downloadUtil.parse(out));
        ossClient.shutdown();
        System.out.print("https://okminerimg.oss-cn-hangzhou.aliyuncs.com/15738963828.png");

    }
}
View Code

 

posted @ 2019-07-22 14:01  枫沫  阅读(290)  评论(0编辑  收藏  举报