0% found this document useful (0 votes)
24 views5 pages

Output Code

This Android application allows users to generate a book art image from an uploaded image file. It gets input from the user via text fields and checkboxes, validates the input, and saves the generated image and HTML code to external storage upon button clicks.

Uploaded by

hi.bot.hunter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Output Code

This Android application allows users to generate a book art image from an uploaded image file. It gets input from the user via text fields and checkboxes, validates the input, and saves the generated image and HTML code to external storage upon button clicks.

Uploaded by

hi.bot.hunter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import android.app.

Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BookArtGenerator extends Activity {


private Canvas canvas, selectedCanvas;
private Paint ctx, selectedCtx;
private Bitmap userImage, selectedPlaceholder;
private String userFileName;
private Map<String, String> entries = new HashMap<>();
private String[] inputIds = {"projectname", "firstpage", "lastpage", "cm",
"inch", "bookheight", "singleprecision", "smoothe"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_art_generator);

Button fileButton = findViewById(R.id.userimage);


fileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadImage();
}
});

for (final String id : inputIds) {


EditText editText = findViewById(getResources().getIdentifier(id, "id",
getPackageName()));
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
processEntries();
}
});
}

final CheckBox smootheCheckBox = findViewById(R.id.smoothe);


smootheCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showValue(smootheCheckBox);
}
});
final TextView smootheOutput = findViewById(R.id.smootheoutput);

Button startButton = findViewById(R.id.startbutton);


startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
processEntries();
}
});

Button imageDownloadButton = findViewById(R.id.imagedlbutton);


imageDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadImage();
}
});

Button textDownloadButton = findViewById(R.id.textdlbutton);


textDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadText();
}
});

canvas = new Canvas();


selectedCanvas = new Canvas();

selectedPlaceholder = Bitmap.createBitmap(200, 200,


Bitmap.Config.ARGB_8888);
Canvas g = new Canvas(selectedPlaceholder);
g.drawColor(Color.GRAY);
displaySelectedImage(null, selectedPlaceholder);
}

private void loadImage() {


// Implement image loading logic
}

private void showValue(CheckBox checkBox) {


TextView smootheOutput = findViewById(R.id.smootheoutput);
smootheOutput.setText(String.valueOf(checkBox.isChecked()));
}

private void processEntries() {


deactivateStartButton();
boolean entriesValid = true;
for (String id : inputIds) {
EditText editText = findViewById(getResources().getIdentifier(id, "id",
getPackageName()));
if (!checkEntry(editText)) {
entriesValid = false;
}
}
entries.put("userimage", userImage.toString());
if (entriesValid) {
activateStartButton();
}
}

private boolean checkEntry(EditText editText) {


editText.setBackgroundColor(Color.WHITE);
String errorMessage = null;
String id = editText.getId() == R.id.projectname ? "projectname" :
editText.getId() == R.id.firstpage ? "firstpage" :
editText.getId() == R.id.lastpage ? "lastpage" :
editText.getId() == R.id.cm ? "cm" :
editText.getId() == R.id.inch ? "inch" :
editText.getId() == R.id.bookheight ? "bookheight" :
editText.getId() == R.id.singleprecision ? "singleprecision" :
editText.getId() == R.id.smoothe ? "smoothe" : null;
switch (id) {
case "projectname":
entries.put("projectname", editText.getText().toString());
break;
case "firstpage":
if (editText.getText().toString().isEmpty() || !
editText.getText().toString().matches("\\d+")) {
errorMessage = "The first page must be an even, positive
number.";
} else {
entries.put("firstpage", editText.getText().toString());
}
break;
case "lastpage":
if (editText.getText().toString().isEmpty() || !
editText.getText().toString().matches("\\d+") ||
Integer.parseInt(editText.getText().toString()) <=
Integer.parseInt(entries.get("firstpage"))) {
errorMessage = "The last page must be an even, positive number.
It must be larger than the number of the first page.";
} else {
entries.put("lastpage", editText.getText().toString());
}
break;
case "cm":
case "inch":
entries.put("unit", id);
break;
case "bookheight":
if (editText.getText().toString().isEmpty() || !
editText.getText().toString().matches("\\d+(\\.\\d{1,1})?")) {
errorMessage = "The book's height must be a positive number
with no more than 1 digit after the decimal point.";
} else {
entries.put("bookheight",
String.valueOf(Float.parseFloat(editText.getText().toString()) * 100));
}
break;
case "singleprecision":
entries.put("singleprecision",
String.valueOf(smootheCheckBox.isChecked()));
break;
case "smoothe":
if (editText.getText().toString().isEmpty() || !
editText.getText().toString().matches("\\d+")) {
errorMessage = "The smoothing value you selected is invalid -
please use a number between 0 and 20.";
} else {
entries.put("smoothe", editText.getText().toString());
}
break;
}
if (errorMessage != null) {
editText.setBackgroundColor(Color.PINK);
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
return false;
}
return true;
}

private void activateStartButton() {


Button startButton = findViewById(R.id.startbutton);
startButton.setEnabled(true);
startButton.setAlpha(1.0f);
}

private void deactivateStartButton() {


Button startButton = findViewById(R.id.startbutton);
startButton.setEnabled(false);
startButton.setAlpha(0.3f);
}

private void displaySelectedImage(View view, Bitmap img) {


img = img == null ? selectedPlaceholder : img;
selectedCanvas.drawColor(Color.TRANSPARENT,
android.graphics.PorterDuff.Mode.CLEAR);
int newWidth, newHeight;
if (img.getWidth() / (double)img.getHeight() < 1) {
newWidth = (int)(img.getWidth() * selectedCanvas.getWidth() /
(double)img.getHeight());
newHeight = selectedCanvas.getHeight();
} else {
newWidth = selectedCanvas.getWidth();
newHeight = (int)(img.getHeight() * selectedCanvas.getWidth() /
(double)img.getWidth());
}
int newX = (selectedCanvas.getWidth() - newWidth) / 2;
int newY = (selectedCanvas.getHeight() - newHeight) / 2;
selectedCanvas.drawBitmap(img, newX, newY, selectedCtx);
}

private void downloadImage() {


Bitmap image = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas g = new Canvas(image);
g.drawBitmap(userImage, 0, 0, null);
String fileName = userFileName.replaceAll("\\.\\w+$", ".png");
File file = new File(Environment.getExternalStorageDirectory(), fileName);
try {
FileOutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Toast.makeText(this, "Image saved to " + file.getAbsolutePath(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}

private void downloadText() {


String projectName = entries.get("projectname");
if (projectName == null || projectName.isEmpty()) {
projectName = "pattern";
}
String htmlCode = "<html><body><!-- Your generated HTML code goes here --
></body></html>";
File file = new File(Environment.getExternalStorageDirectory(),
projectName.replace(" ", "_").toLowerCase() + ".html");
try {
FileOutputStream out = new FileOutputStream(file);
out.write(htmlCode.getBytes());
out.flush();
out.close();
Toast.makeText(this, "HTML code saved to " + file.getAbsolutePath(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like