从创建Java的帆布位图 [英] Creating bitmap from canvas java
问题描述
SOLUTION
感谢@钱德拉塞卡的建议的问题是,我是在传递一个不变的位图到画布的构造函数。解决的办法是使用BitmapFactory.de codeFILE()时创建一个副本;
BMP位图= BitmapFactory.de codeFILE(IMAGEURL).copy(Bitmap.Config.ARGB_8888,真);
所以,我有我使用bitmapFactory.de codeFILE()的位图和工作原理。我能够创建位图,然后我需要创建一个画布,这就是事情变得怪异。
下面是发生了什么事的流动。
我拍摄图像,然后将它传递到泛函是其大小,节省出来,并返回其文件路径。 (我用的PhoneGap科尔多瓦)
然后我传递URL回到我的Java和使用previously保存的图像和操纵它functionB
code题:
//获取URL TO IMAGE
最终的JSONObject选项= optionsArr.optJSONObject(0);
字符串IMAGEURL = options.optString(图像);//创建位图图像
BMP位图= BitmapFactory.de codeFILE(IMAGEURL);
BMP = Bitmap.createBitmap(BMP,0,0,655,655);/ *一切正常,直到这点* ///创建图像画布
帆布帆布=新的Canvas(BMP);
位图1 = Bitmap.createBitmap(BMP);
canvas.drawBitmap(一,0,0,空);
我收到任何错误,它只是挂起。下面是在裤子踢 - 如果我再跑功能说functionB第一个作品,但其他没有
。我想也许我需要刷新,关闭我的第一个FileOutputStream中,但似乎没有任何效果。我已经尝试了不同的变量名的所有元素,位图,画布,fileoutputstreams。
这里是全功能的例子...
注:由于我使用的PhoneGap /科尔多瓦我返回一个字符串
公共字符串无(JSONArray optionsArr){ //设置文件路径
串文件路径=;
文件路径=新的文件(Environment.getExternalStorageDirectory()+/对myApp /); // TMP.jpg是我们存储了图像的临时版本
文件NewFilePath =新的文件(路径,tmp_NBB.jpg); //创建文件夹如果NEEDED
尝试{
布尔成功= FALSE; 如果(!path.exists()){
成功= path.mkdir();
}
如果(!成功){
Log.d(NONE,未创建文件夹。);
}
其他{
Log.d(NONE,创建的文件夹!);
}
}
赶上(例外五){
e.printStackTrace();
}
//获取URL TO IMAGE
最终的JSONObject选项= optionsArr.optJSONObject(0);
字符串IMAGEURL = options.optString(图像); //创建位图图像
BMP位图= BitmapFactory.de codeFILE(IMAGEURL);
BMP = Bitmap.createBitmap(BMP,0,0,655,655); //创建图像画布
帆布帆布=新的Canvas(BMP);
位图无= Bitmap.createBitmap(BMP);
canvas.drawBitmap(无,0,0,NULL); // 保存图片
尝试{
//输出流
FileOutputStream中出=新的FileOutputStream(NewFilePath);
none.com preSS(Bitmap.Com pressFormat.JPEG,100,出); //获取文件路径
URI URI = Uri.fromFile(NewFilePath);
文件路径= uri.toString(); 尝试{
了out.flush();
out.close(); //返回文件路径
返回文件路径;
}
赶上(例外五){
e.printStackTrace();
}
}赶上(例外五){
e.printStackTrace();
}
返回文件路径;
}
就像我说的这个工程的第一个图像,但是当我试图再次打开这个形象的基础上,返回文件路径它在创建帆布行豆腐块了。
编辑:映像路径我使用看起来像这样:
/mtn/sdcard/myApp/tmp.jpg
什么想法?
位图1 = Bitmap.createBitmap(BMP);
在上面的code BMP 是一个位图,并正在创建另一个位图对象的有一个从 BMP
删除线,并通过改变试
canvas.drawBitmap(一,0,0,NULL);
到
canvas.drawBitmap(BMP,0,0,NULL);
SOLUTION
Thanks to @ChandraSekhar's suggestions the issue was that I was passing in an Immutable Bitmap to the canvas constructor. The solution is to create a copy of it when using BitmapFactory.decodeFile();
Bitmap bmp = BitmapFactory.decodeFile(imageURL).copy(Bitmap.Config.ARGB_8888, true);
So I have a bitmap that I am using bitmapFactory.decodeFile() for and this works. I am able to create the bitmap, then I need to create a canvas and this is where things get weird.
Here's the flow of what is happening. I capture an image, then pass it to functionA that sizes it, and saves it out and returns its file path. ( I am using Phonegap Cordova )
I then pass that URL back to my java and use the previously saved image and manipulate it in functionB
CODE IN QUESTION:
// GET URL TO IMAGE
final JSONObject options = optionsArr.optJSONObject(0);
String imageURL = options.optString("image");
// create image bitmap
Bitmap bmp = BitmapFactory.decodeFile(imageURL);
bmp = Bitmap.createBitmap(bmp,0,0,655,655);
/* Everything works fine until this point */
// create image canvas
Canvas canvas = new Canvas(bmp);
Bitmap one = Bitmap.createBitmap(bmp);
canvas.drawBitmap(one,0,0,null);
I receive no errors, it just hangs. Here's the kick in the pants - if I run another function say functionB first that one works but the other doesn't.
I thought maybe I needed to flush and close my first FileOutputStream, but that didn't seem to have any effect. I've tried different variable names for all elements, bitmaps, canvas, and fileoutputstreams.
here is an example of the full function... NOTE: Because I am using phonegap / cordova I am returning a string
public String none(JSONArray optionsArr) {
// SET FILE PATH
String filePath = "";
File path = new File(Environment.getExternalStorageDirectory()+"/myApp/");
// TMP.jpg is where we store our temporary version of the image
File NewFilePath = new File(path, "tmp_NBB.jpg");
// CREATE FOLDERS IF NEEDED
try{
boolean success = false;
if(!path.exists()){
success = path.mkdir();
}
if (!success){
Log.d("NONE","Folder not created.");
}
else{
Log.d("NONE","Folder created!");
}
}
catch (Exception e){
e.printStackTrace();
}
// GET URL TO IMAGE
final JSONObject options = optionsArr.optJSONObject(0);
String imageURL = options.optString("image");
// create image bitmap
Bitmap bmp = BitmapFactory.decodeFile(imageURL);
bmp = Bitmap.createBitmap(bmp,0,0,655,655);
// create image canvas
Canvas canvas = new Canvas(bmp);
Bitmap none = Bitmap.createBitmap(bmp);
canvas.drawBitmap(none,0,0,null);
// SAVE IMAGE
try {
// OUTPUT STREAM
FileOutputStream out = new FileOutputStream(NewFilePath);
none.compress(Bitmap.CompressFormat.JPEG, 100, out);
// GET FILE PATH
Uri uri = Uri.fromFile(NewFilePath);
filePath = uri.toString();
try{
out.flush();
out.close();
// RETURN FILE PATH
return filePath;
}
catch (Exception e){
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return filePath;
}
Like I said this works for the first image, but when I attempt to open this image again, based on the returned filepath it chunks out at the create canvas line.
edit: The image path I am using looks like this: /mtn/sdcard/myApp/tmp.jpg thoughts?
Bitmap one = Bitmap.createBitmap(bmp);
In the above code bmp is a Bitmap and you are creating another Bitmap object one from bmp.
Remove that line and try by changing
canvas.drawBitmap(one,0,0,null);
to
canvas.drawBitmap(bmp,0,0,null);
这篇关于从创建Java的帆布位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!