Android Zapisz widok do jpg lub png
Chciałbym napisać aplikację na Androida, która zasadniczo nakłada nakładkę na obraz na innym obrazie, a następnie chciałbym zapisać zdjęcie z nakładką jako jpg lub png. Zasadniczo będzie to cały widok, który chciałbym zapisać.
Przykładowy kod byłby bardzo pomocny.
EDIT:
Wypróbowałem Twoje sugestie i dostaję wskaźnik null na linii gwiazdek.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.widget.LinearLayout;
import android.widget.TextView;
public class EditPhoto extends Activity {
/** Called when the activity is first created. */
LinearLayout ll = null;
TextView tv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.text);
ll = (LinearLayout) findViewById(R.id.layout);
ll.setDrawingCacheEnabled(true);
Bitmap b = ll.getDrawingCache();
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
*** b.compress(CompressFormat.JPEG, 95,fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
39
Author: shaneburgess, 2010-06-24
3 answers
Możesz skorzystać z pamięci podręcznej rysowania widoku.
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));
Gdzie widok jest Twoim widokiem. 95 to jakość kompresji JPG. A strumień wyjściowy pliku jest właśnie tym.
79
Author: Moncader,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-20 03:04:45
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-20 03:04:45
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos = new FileOutputStream(file);
Użyj referencji fos jako Trzeciego parametru metody B. compress() w odpowiedzi Moncadera. Obraz zostanie zapisany jako obraz.jpg w katalogu głównym karty sd.
6
Author: plugmind,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-23 19:31:46
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-23 19:31:46
Zapisz RelativeLayout lub dowolny widok do obrazu (.jpg)
String getSaveImageFilePath() {
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), YOUR_FOLDER_NAME);
// Create a storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(YOUR_FOLDER_NAME, "Failed to create directory");
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "IMG_" + timeStamp + ".jpg";
String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
Log.d(YOUR_FOLDER_NAME, "selected camera path " + selectedOutputPath);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
int maxSize = 1080;
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
if (bWidth > bHeight) {
int imageHeight = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
bitmap = Bitmap.createScaledBitmap(bitmap, maxSize, imageHeight, true);
} else {
int imageWidth = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
bitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, maxSize, true);
}
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
OutputStream fOut = null;
try {
File file = new File(selectedOutputPath);
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return selectedOutputPath;
}
1
Author: Harikesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-03 12:17:04
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-03 12:17:04