2
回答
Java base64图片编码上传,判断图片是否有旋转,修正并保存
注册华为云得mate10,2.9折抢先购!>>>   

最近遇到一个问题,手机端上传的图片为base64编码格式的,服务器需要通过解码字符串然后判断图片是否有旋转角度,在保存为图片。手上有一个.net的例子,但需要转为Java语言的,由于对Java的类认知不够,故不能完成.net程序转换为Java程序。各位大神,求指点。

以下是.net案例:

/// <summary>
        /// Base64解码字符串,并生成对应的文件保存
        /// </summary>
        /// <param name="imgStr">Base64解码字符串</param>
        /// <param name="filePath">保存路径</param>
        /// <param name="fileName">保存文件名</param>
        /// <returns></returns>
        protected bool StringToImage(string imgStr, string filePath, string fileName)
        {
            if (imgStr == null) return false;
            try
            {
                byte[] bts = Convert.FromBase64String(imgStr);
                using (System.IO.FileStream fs = new System.IO.FileStream(filePath + fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite))
                {
                    foreach (var b in bts)
                    {
                        fs.WriteByte(b);
                    }
                    //判断图片旋转角度并进行校正
                    var bmp = new System.Drawing.Bitmap(fs);
                    //此处引用了一个类库,用以获取照片的Exif信息
                    Exif.Exifextractor exif = new Exif.Exifextractor(ref bmp, "\n");
                    var ori = exif["orientation"];
                    if (ori != null)
                    {
                        switch (ori.ToString())
                        {
                            case "3"://要进行180度旋转
                                bts = BitmapToByte(KiRotate(bmp, 180, Color.Transparent));
                                break;
                            case "6"://要进行90度旋转
                                bts = BitmapToByte(KiRotate(bmp, 90, Color.Transparent));
                                break;
                            case "8"://要进行-90度旋转
                                bts = BitmapToByte(KiRotate(bmp, -90, Color.Transparent));
                                break;
                        }
                        fs.Seek(0, SeekOrigin.Begin);
                        foreach (var b in bts)
                        {
                            fs.WriteByte(b);
                        }
                    }
                    fs.Flush();
                    fs.Close();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }



/// <summary>
        /// 将bmp图片转换成byte
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private byte[] BitmapToByte(Bitmap bitmap)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Jpeg);
                byte[] data = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(data, 0, Convert.ToInt32(stream.Length));
                return data;
            }
        }



/// <summary>
        /// 任意角度旋转
        /// </summary>
        /// <param name="bmp">原始图Bitmap</param>
        /// <param name="angle">旋转角度</param>
        /// <param name="bkColor">背景色</param>
        /// <returns>输出Bitmap</returns>
        private Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
        {
            int w = bmp.Width + 2;
            int h = bmp.Height + 2;

            PixelFormat pf;

            if (bkColor == Color.Transparent)
            {
                pf = PixelFormat.Format32bppArgb;
            }
            else
            {
                pf = bmp.PixelFormat;
            }

            Bitmap tmp = new Bitmap(w, h, pf);
            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, 1, 1);
            g.Dispose();

            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(new RectangleF(0f, 0f, w, h));
            Matrix mtrx = new Matrix();
            mtrx.Rotate(angle);
            RectangleF rct = path.GetBounds(mtrx);

            Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
            g = Graphics.FromImage(dst);
            g.Clear(bkColor);
            g.TranslateTransform(-rct.X, -rct.Y);
            g.RotateTransform(angle);
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.DrawImageUnscaled(tmp, 0, 0);
            g.Dispose();

            tmp.Dispose();

            return dst;
        }



举报
顶部