Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
XChart
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brady James Garvin
XChart
Commits
89e2fad0
Commit
89e2fad0
authored
11 years ago
by
Tim Molter
Browse files
Options
Downloads
Patches
Plain Diff
cleaned up, added 2 methods
parent
89741aca
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java
+31
-12
31 additions, 12 deletions
xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java
with
31 additions
and
12 deletions
xchart/src/main/java/com/xeiam/xchart/BitmapEncoder.java
+
31
−
12
View file @
89e2fad0
...
...
@@ -18,6 +18,7 @@ package com.xeiam.xchart;
import
java.awt.Graphics2D
;
import
java.awt.geom.AffineTransform
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
...
...
@@ -58,9 +59,7 @@ public final class BitmapEncoder {
*/
public
static
void
savePNG
(
Chart
chart
,
String
fileName
)
throws
IOException
{
BufferedImage
bufferedImage
=
new
BufferedImage
(
chart
.
getWidth
(),
chart
.
getHeight
(),
BufferedImage
.
TYPE_INT_RGB
);
Graphics2D
graphics2D
=
bufferedImage
.
createGraphics
();
chart
.
paint
(
graphics2D
);
BufferedImage
bufferedImage
=
getBufferedImage
(
chart
);
// Save chart as PNG
OutputStream
out
=
new
FileOutputStream
(
fileName
);
...
...
@@ -146,15 +145,13 @@ public final class BitmapEncoder {
*
* @param chart
* @param fileName
* @param quality -
//
a float between 0 and 1 (1 = maximum quality)
* @param quality - a float between 0 and 1 (1 = maximum quality)
* @throws FileNotFoundException
* @throws IOException
*/
public
static
void
saveJPG
(
Chart
chart
,
String
fileName
,
float
quality
)
throws
FileNotFoundException
,
IOException
{
BufferedImage
bufferedImage
=
new
BufferedImage
(
chart
.
getWidth
(),
chart
.
getHeight
(),
BufferedImage
.
TYPE_INT_RGB
);
Graphics2D
graphics2D
=
bufferedImage
.
createGraphics
();
chart
.
paint
(
graphics2D
);
BufferedImage
bufferedImage
=
getBufferedImage
(
chart
);
Iterator
<
ImageWriter
>
iter
=
ImageIO
.
getImageWritersByFormatName
(
"jpeg"
);
ImageWriter
writer
=
iter
.
next
();
...
...
@@ -170,12 +167,34 @@ public final class BitmapEncoder {
writer
.
dispose
();
}
public
static
void
main
(
String
[]
args
)
{
/**
* Generates a byte[] for a given chart, PNG compressed
*
* @param chart
* @return a byte[] for a given chart, PNG compressed
* @throws IOException
*/
public
static
byte
[]
getPNGBytes
(
Chart
chart
)
throws
IOException
{
BufferedImage
bufferedImage
=
getBufferedImage
(
chart
);
byte
[]
imageInBytes
=
null
;
for
(
String
format
:
ImageIO
.
getWriterFormatNames
())
{
System
.
out
.
println
(
format
);
// ImageIO.write(bufferedImage, format, new File("C:\\image_new." + format));
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ImageIO
.
write
(
bufferedImage
,
"png"
,
baos
);
baos
.
flush
();
imageInBytes
=
baos
.
toByteArray
();
baos
.
close
();
return
imageInBytes
;
}
public
static
BufferedImage
getBufferedImage
(
Chart
chart
)
{
BufferedImage
bufferedImage
=
new
BufferedImage
(
chart
.
getWidth
(),
chart
.
getHeight
(),
BufferedImage
.
TYPE_INT_RGB
);
Graphics2D
graphics2D
=
bufferedImage
.
createGraphics
();
chart
.
paint
(
graphics2D
);
return
bufferedImage
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment