android对图片的操作

1.保存图片到本地

  /**
     * 保存matmap
     */
    public static void saveBitmap(String name,Bitmap bm) {
        Log.e(TAG, "保存图片");
        File f = new File(name);

            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 80, out);
            out.flush();
            out.close();
            Log.i("gifname", "已经保存 "+f.getName());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
2.保留nv21格式缩放bitmap
    /**
     * 缩放bitmap
     * @param bitmap
     * @param width 要缩放的宽和高
     * @param height
     * @return
     */
    public static  byte[] zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

        byte[] datas = getNV21(width,height,newbmp);
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        newbmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//        byte[] datas = baos.toByteArray();
        return datas;
    }

public static byte[] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {

        int[] argb = new int[inputWidth * inputHeight];

        scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);

        byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
        encodeYUV420SP(yuv, argb, inputWidth, inputHeight);

        scaled.recycle();

        return yuv;
    }

    public static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
        final int frameSize = width * height;
        int yIndex = 0;
        int uvIndex = frameSize;
        int a, R, G, B, Y, U, V;
        int index = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
                a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
                R = (argb[index] & 0xff0000) >> 16;
                G = (argb[index] & 0xff00) >> 8;
                B = (argb[index] & 0xff) >> 0;
                // well known RGB to YUV algorithm
                Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
                U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
                V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

                // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
                //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
                //    pixel AND every other scanline.
                yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
                if (j % 2 == 0 && index % 2 == 0) {
                    yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
                    yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
                }
                index++;
            }
        }
    }

3.byte[]转bitmap

 /**
     * byte[]转bitmap
     * @param bs
     * @return
     */
    public static Bitmap Bytes2Bimap(byte[] bs,int w,int h) {
        YuvImage yuvimage=new YuvImage(bs, ImageFormat.NV21, w,h, null);//20、20分别是图的宽度与高度
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(0, 0,w, h), 80, baos);//80--JPG图片的质量[0-100],100最高
        byte[] jdata = baos.toByteArray();
        Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
        return bmp;
    }

4.bitmap生产gif

 public static void createGif(ArrayList<Bitmap> bitmaps, int fps, String path)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
        String dateString = simpleDateFormat.format(new Date());
        String gifName = path+"/"+ dateString+".gif";


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileOutputStream fos = null;
        AnimatedGifEncoder localAnimatedGifEncoder = new AnimatedGifEncoder();
        String date = path+"/"+new Date().getTime();
        File file2 = new File(date);
        if(!file2.exists()){
            file2.mkdirs();
        }
        localAnimatedGifEncoder.start(baos);//start
            int i = 0;
            for (Bitmap bitmap : bitmaps) {
                i++;
                localAnimatedGifEncoder.addFrame(bitmap);

                saveBitmap(date+"/"+i+".jpg",bitmap);
            }
        localAnimatedGifEncoder.setRepeat(0);//设置生成gif的开始播放时间。0为立即开始播放
        localAnimatedGifEncoder.setDelay(fps);
        localAnimatedGifEncoder.finish();//finish
        try {
            File file = new File(path);
            if (!file.exists())
                file.mkdirs();

            Log.d("gifname","gifname : "+gifName);
            File gifFile = new File(gifName);
            if(!gifFile.exists())
                try {
                    gifFile.createNewFile();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            fos = new FileOutputStream(gifFile);
            baos.writeTo(fos);
            baos.flush();
            fos.flush();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            try {
                baos.close();
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

猜你喜欢

转载自blog.csdn.net/u012539700/article/details/79988844
今日推荐