I have few classes and functions in java code.
one of the function is drawing a graph using graphics object.
I want to save this graph in jpg format so i did:
public void paintComponent(Graphics graphics) {
Graphics g1=null;
double scale = ((double) ySize) / histogram.getMaxFrequency(band);
if (histColor != null)
graphics.setColor(histColor);
for (int x = 0; x < 256; ++x) {
int y = (int) Math.round(scale*histogram.getFrequency(band, x));
if (y > 0)
graphics.drawLine(x+xOrigin, yOrigin, x+xOrigin, yOrigin-y);
}
FileOutputStream fileopstream;
try {
fileopstream = new FileOutputStream("C:\\Users\\spider\\Desktop\\Study\\Computer Vision\\programs\\histogram1.jpg");
JPEGImageEncoder output=JPEGCodec.createJPEGEncoder(fileopstream);
BufferedImage image= new BufferedImage(100,100,BufferedImage.TYPE_BYTE_BINARY);
//WritableRaster raster=image.getRaster();
g1=image.createGraphics();
g1=graphics.create();
//g1.dispose();
//graphics.drawImage(image, 0, 0, 100, 100, null, null);
//output.encode(image);
// Save as JPEG
File file = new File("newimage.jpg");
ImageIO.write(image, "jpg", file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
drawAxis(graphics, xOrigin, yOrigin+1);
}
but it is not working. The stored image is a blank image and not the graph which i can see on screen.
can you pls help me
thanks a lot
got the answer :)
checked:
http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html#comment-83262
Related
Barcode I want to send a barcode to the printer. I tried it in PDF, JPEG, PNG format. It prints a blank page on all of them.I couldn't find any results on google.
I need to do it without using Printer Helper, if possible
public class yazdır extends AsyncTask<Void , Void,Void> {
#Override
protected Void doInBackground(Void... arg0) {
DataOutputStream outToServer;
Socket clientSocket;
File filename;
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
for (int i=0;i<=adet;i++) {
try {
String path = Environment.getExternalStorageDirectory().toString();
filename = new File(path + "/folder/subfolder/image.png");
FileInputStream fileInputStream = new FileInputStream(filename);
InputStream is = fileInputStream;
clientSocket = new Socket("192.168.1.31", 9100);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
byte[] buffer = new byte[1024];
int size;
while ((size=is.read(buffer)) !=-1){
outToServer.write(buffer,0,size);
}
outToServer.flush();
} catch (ConnectException connectException) {
Log.e(TAG, connectException.toString(), connectException);
return null;
} catch (UnknownHostException e1) {
Log.e(TAG, e1.toString(), e1);
return null;
} catch (IOException e1) {
Log.e(TAG, e1.toString(), e1);
return null;
} finally {
}
}
return null;}
}
When I send it to a regular printer. Says document is printing and cancels printing.
i'm developing J2ME.
Could you mind helping me to find out whats wrong with this code:
HttpConnection c = null;
try {
c = (HttpConnection)Connector.open("http://www.mysite.com",Connector.READ_WRITE, true);
c.setRequestMethod(HttpConnection.GET); //default
is = c.openInputStream(); // transition to connected!
int ch = 0;
for(int ccnt=0; ccnt < 150; ccnt++) { // get the title.
ch = is.read();
if (ch == -1){
break;
}
sb.append((char)ch);
}
}
catch (IOException x){
x.printStackTrace();
}
finally{
try {
is.close();
c.close();
} catch (IOException x){
x.printStackTrace();
}
}
System.out.println(sb.toString());
i'm trying open website through my app, thanks before :)
To open a URL in J2ME you do the following in a non-system thread:
MIDlet.platformRequest("http://www.mysite.com/");
I am trying to stream an audio content (AAC) from server. The player starts without any problem, and after some time throws OutOfMemoryError. I think I need to buffer the audio. Any one please guide me to implement (or get rid-of this error) buffering.
Here is my code.
`HttpConnection c = null;
InputStream is = null;
try {
c = (HttpConnection) Connector.open(streamURL,
Connector.READ_WRITE);
int rc = c.getResponseCode();
System.out.println("Response Code " + rc);
if (rc != HttpConnection.HTTP_OK) {
showAlert("Server error returned");
} else {
playerForm.append("openInputStream");
is = c.openInputStream();
player = Manager.createPlayer(is, "audio/aac");
player.realize();
// get volume control for player and set volume to max
VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
if(vc != null)
{
vc.setLevel(100);
}
player.prefetch();
player.start();
}
} catch (Exception f) {
playerForm.append("Exception in player " + f.getMessage() + "\n");
} finally { // Aufräumarbeiten ausführen
if (is != null)
try {
is.close();
} catch (IOException e) {
}
if (c != null)
try {
c.close();
} catch (IOException e) {
}
}`
Thanks in advance.
Regards
Anish.
Instead of Exception in catch just use Throwable class it will handle OutOfMemoryError
In my application we have to download around 10 Images from server and display it in mobile. How can I do this? Can I use same HttpConnection for full download? Is any other way for download?
You can do with this simple loop (Supposing imageList is a List with the url of the images).
HttpConnection = null;
Image image = null;
for (int i = 0; i < imageList.getSize(); i++) {
try{
String urlImage = imageList.get(i);
hc = (HttpConnection) Connector.open(urlImage);
image = Image.createImage(hc.openInputStream()));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
hc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
You can try following method in a loop for downloading images from server.
private void downloadImage ( String URL )
{
try
{
System.out.println("URL FOR POST_DATA : "+URL);
// Open up a http connection with the Web server for both send and receive operations
httpConnection = (HttpConnection)Connector.open(URL, Connector.READ_WRITE);
// Set the request method to POST
httpConnection.setRequestMethod(HttpConnection.POST);
// Set the request headers
httpConnection.setRequestProperty(ConstantCodes.ACTION_MODE_PARAMETER,action);
httpConnection.setRequestProperty(ConstantCodes.USER_NAME_REQUEST_PARAMETER,userName);
if(eventName==null || eventName.equals(""))
eventName="default";
httpConnection.setRequestProperty(ConstantCodes.EVENT_NAME_REQUEST_PARAMETER, eventName);
httpConnection.setRequestProperty(ConstantCodes.CAMERAID_REQUEST_PARAMETER, cameraID);
// all the headers are sending now and connection chanel is establising
dis = httpConnection.openDataInputStream();
int ch = 0;
ByteArrayOutputStream bytearray = new ByteArrayOutputStream(250000);
while((ch = dis.read()) != -1)
bytearray.write(ch);
// fileByte contains whole file in bytes
byte fileByte[] = bytearray.toByteArray();
fileSize = fileByte.length;
System.out.println("Got file size : "+fileSize);
if(bytearray!=null) bytearray.close();
midlet.getLastPostedImageResponse(fileByte);
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.out.println("IOException occured during getting last image data : "+ioe.getMessage());
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Eeception occurred during getting last image data : "+e.getMessage());
}
finally
{
System.out.println("Calling close from Last image posted Action");
close();
}
I'm trying to play a recorded wave file. While playing, an exception is thrown at the following statement:
Player player = Manager.createPlayer(is, "audio/mpeg");
My entire code for playing the wave file is as follows:
if (types[cnt].equals("audio/x-wav")) {
Class clazz = Class.forName("RecordAudio");
InputStream is =
clazz.getResourceAsStream("file:///SDCard/BlackBerry/original.wav");
//create an instance of the player from the InputStream
Player player = Manager.createPlayer(is, "audio/mpeg");
player.realize();
player.prefetch();
//start the player
player.start();
}
What could be the problem?
The function getResourceAsStream is for pulling resources from a JAR/COD file, not from the filesystem. Plus, this is simpler than you are making it. Just pass the file name and path to createPlayer, like so:
try {
String filename = "file:///SDCard/BlackBerry/original.wav";
Player player = javax.microedition.media.Manager.Manager.createPlayer( filename );
} catch (IOException e) {
System.out.println("Error creating player");
} catch (MediaException e) {
System.out.println("Error media type");
}
I believe it is because of wrong MIME type. Try this:
String fileName = "file:///SDCard/BlackBerry/original.wav";
String mimeType = "audio/x-wav";
String types[] = javax.microedition.media.Manager
.getSupportedContentTypes(null);
for (int cnt = types.length - 1; cnt >= 0; --cnt) {
if (types[cnt].equals(mimeType)) {
InputStream is = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(
fileName, Connector.READ);
} catch (IOException e) {
System.out.println("Error reading file");
}
try {
is = fconn.openInputStream();
} catch (IOException e) {
System.out.println("Error opening stream");
}
Player player = null;
try {
player =
javax.microedition.media.Manager.createPlayer(
is, mimeType);
} catch (IOException e) {
System.out.println("Error creating player");
} catch (MediaException e) {
System.out.println("Error media type");
}
try {
player.realize();
} catch (MediaException e) {
System.out.println("Player cannot be released");
}
try {
player.prefetch();
} catch (MediaException e) {
System.out.println("Player cannot be prefetched");
}
// start the player
try {
player.start();
} catch (MediaException e) {
System.out.println("Player cannot be started");
}
}
}
Also see in console what kind of exception was thrown.