How to create a PowerPoint from scratch with Apache POI? - apache-poi

I can't seem to create a PPT like this. Any help is appreciated. Thanks.
public static void main(String[] args) throws Exception{
File file=new File("/Users/Rakuten/Desktop/Test/example2.ppt");
try {
FileOutputStream out= new FileOutputStream(file);
SlideShowFactory.create(file);
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}

Related

Bitmap compress quality ok in emulator, but not in real device

In my app, images are saving successfully to gallery after editing. but quality is not up to the mark on physical device. I got 0.5mp to 0.7mp highest. but same app I open in emulator and after saving image I got pretty good quality of images (about 1.5mp to 3mp). didn't find the exact reason of this. will be glad if you help to find out. attaching my image saving code below.
public void saveAsFile(#NonNull final String str, #NonNull final SaveSettings saveSettings, #NonNull final OnSaveListener onSaveListener) {
Log.d(TAG, "Image Path: " + str);
this.parentView.saveFilter((OnSaveBitmap) new OnSaveBitmap() {
#Override
public void onBitmapReady(Bitmap bitmap) {
new AsyncTask<String, String, Exception>() {
#Override
public void onPreExecute() {
super.onPreExecute();
PhotoEditor.this.clearHelperBox();
PhotoEditor.this.parentView.setDrawingCacheEnabled(false);
}
#SuppressLint({"MissingPermission"})
public Exception doInBackground(String... strArr) {
Bitmap bitmap;
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File(str), false);
if (PhotoEditor.this.parentView != null) {
PhotoEditor.this.parentView.setDrawingCacheEnabled(true);
if (saveSettings.isTransparencyEnabled()) {
bitmap = BitmapUtil.removeTransparency(PhotoEditor.this.parentView.getDrawingCache());
} else {
bitmap = PhotoEditor.this.parentView.getDrawingCache();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100 , fileOutputStream);
}
Log.d(PhotoEditor.TAG, "Filed Saved Successfully");
return null;
} catch (Exception e) {
e.printStackTrace();
Log.d(PhotoEditor.TAG, "Failed to save File");
return e;
}
}
#Override
public void onPostExecute(Exception exc) {
super.onPostExecute(exc);
if (exc == null) {
if (saveSettings.isClearViewsEnabled()) {
PhotoEditor.this.clearAllViews();
}
onSaveListener.onSuccess(str);
return;
}
onSaveListener.onFailure(exc);
}
}.execute();
}
public void onFailure(Exception exc) {
onSaveListener.onFailure(exc);
}
});
}
I tried in many ways but couldn't find the solution.

updating chart real time Cross-thread operation not valid

I'm trying to build a GUI for my application. I'm stuck on one problem. I try to use timer "OnTImedEvent" to call my function to update the chart. Unfortunately, MVS gives me a "Cross-thread operation not valid". I've came across some tips, regarding something called delegate but I can't manage to get it working. Since I'm nooby in windowsFormsApp (started today) I'm asking for your help. Here's my code:
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
bool state = false;
private static System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer(10);
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("a\n");
}
else
MessageBox.Show("Brak połączenia z urządzeniem");
}
private void button4_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
Form2 form2 = new Form2(serialPort1);
form2.Show();
}
else
MessageBox.Show("Brak połączenia z urządzeniem");
}
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
comBoxPort.Items.AddRange(ports);
}
private void btnCon_Click(object sender, EventArgs e)
{
if (state)
{
state = false;
btnCon.ForeColor = Color.Red;
try
{
serialPort1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
else
{
state = true;
btnCon.ForeColor = Color.Green;
try
{
serialPort1.PortName = comBoxPort.Text;
serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
}
private void btnRoll_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("s\n");
}
else
MessageBox.Show("Brak połączenia z urządzeniem");
}
private void btnYAW_Click(object sender, EventArgs e)
{
drawChart();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("d\n");
}
//else
//MessageBox.Show("Brak połączenia z urządzeniem");
}
private void drawChart()
{
chart1.Series["PITCH"].Points.AddY(21);
chart1.Series["ROLL"].Points.AddY(122);
chart1.Series["YAW"].Points.AddY(13);
}
public delegate void drawChartCallback();
private void chart1_Click(object sender, EventArgs e)
{
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
drawChart();
}
}
}
Thanks in advance for your help.
While its not the best way to do it, for a beginner the easist way to avoid the problem is to use the following variable form.CheckForIllegalCrossThreadCalls = false;

I get an error when I try to play a sound file

I have the following code that I copied from the package playsound because I would like to ask you what is the problem:
public class Playsound extends JFrame {
public Playsound() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test Sound Clip");
this.setSize(300, 200);
this.setVisible(true);
try {
URL url = this.getClass().getClassLoader().getResource("waterdrop.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Playsound();
}
}
But when I try to run it, I get:
Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:207)
at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:841)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:178)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1147)
at playsound.Playsound.<init>(Playsound.java:18)
at playsound.Playsound.main(Playsound.java:32)
It dies at this row:
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
I have this setup in the folder Image
So I would like to ask that why is it not working? Maybe it is me, maybe it is not but it does not work and it is not nice.

Breakpoint not working inside a Handler - Android Studio

All the other breakpoints inside my class work just fine.
However, none of the breakpoints inside the run method. What can the issue be?
private Emitter.Listener onConnect = new Emitter.Listener() {
#Override
public void call(final Object...args) {
new Handler(getMainLooper()).post(new Runnable() {
#Override
public void run() {
Toast.makeText(ChatService.this, "connected", Toast.LENGTH_SHORT).show();
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String json = gson.toJson(user);
try {
obj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
mSocket.emit("set-user-data", obj, user.getRole());
JSONArray membersJSONArray = new JSONArray();
membersJSONArray.put(user.getUsername());
JSONObject roomInfo = new JSONObject();
try {
roomInfo.put("members", membersJSONArray);
roomInfo.put("platformInfo", user.getPlatformInfo());
} catch (JSONException e) {
e.printStackTrace();
}
mSocket.emit("set-room", roomInfo);
}
});
}
};

How do i make an android app that can record and listen at the same time

I have trouble making an app that can record and listen at the same time. I have create a recorder that is able to record and play at the moment,but i would really appreciate if someone could give suggestion on how to get started with android studio so that i can make the app to record and listen at the same time.
stop.setEnabled(false);
play.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrec.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
}
public void start(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
}catch (IllegalStateException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
start.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(this,"Recording started",Toast.LENGTH_SHORT).show();
}
public void stop(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder=null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(this,"Audio successfully recorded",Toast.LENGTH_SHORT).show();
}
public void play(View v) throws IOException {
MediaPlayer m=new MediaPlayer();
m.setDataSource(outputFile);
m.prepare();
m.start();
Toast.makeText(this,"Playing audio", Toast.LENGTH_SHORT).show();
}
}

Resources